不同节行中的iOS Tableview单元格具有相同的布局



我有一个问题,最好的描述方法是给你看图片。该节的最后一行应具有光泽样式,文本居中对齐。首先,我有一张桌子,上面有这样一个部分:

http://image-upload.de/image/RL93lJ/58f2422d97.png

但当我在顶部添加另一个部分时,我会得到这样的东西:

http://image-upload.de/image/JggrVq/6ee42a1089.png

以下是缩写代码:

static NSString *CellIdentifier = @"CellOrder";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cellIsLastOne == TRUE) {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        //name

        cell.textLabel.text = @"Bezahlt";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:25];
        cell.textLabel.textColor =  [UIColor whiteColor];
        //Glossy
        CALayer *thisLayer = cell.layer;
        if (thisLayer.sublayers.count == 1) {
            CAGradientLayer *glossLayer = [CAGradientLayer layer];
            glossLayer.frame = CGRectMake(cell.bounds.origin.x + cellOffset, cell.bounds.origin.y, self.tableView.frame.size.width - cellOffset * 2, cellItemHeight);
            glossLayer.colors = [NSArray arrayWithObjects:
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 (id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 nil];
            glossLayer.locations = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:0.0f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:1.0f],
                                    nil];
            [thisLayer addSublayer:glossLayer];
        }
    } else {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        ItemID;
        cell.textLabel.text = @"Name";
        cell.detailTextLabel.text = @"Price";
        cell.detailTextLabel.textColor = [UIColor whiteColor];

        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.detailTextLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.textLabel.textColor =  [UIColor whiteColor];
    }
    cell.detailTextLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.detailTextLabel.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.shadowOffset = CGSizeMake(0, 2);
    cell.backgroundColor = [UIColor colorWithRed:0.72 green:0.76 blue:0.03 alpha:1.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    return cell;

我真的不明白这个问题。请帮帮我。谢谢!)

好吧,您的屏幕截图显示的问题表明您对cellIsLastOne的计算是错误的。

但是,即使它是正确的,当你滚动浏览表视图时,这也会有很多问题,因为单元格正在被重复使用。要修复,您可以:

  • 对光泽单元格使用不同的重复使用标识符
  • 移除else块中的光泽子层

基本上,当您调用dequeueReusableCellWithIdentifier:时,它可能会返回一个先前添加了光泽层的单元。此外,这可能会多次添加光泽层。

我编辑您的代码,这可能会对您有所帮助。

    static NSString *CellIdentifier = @"CellOrder";
    static NSString *glowCellIdentifier = @"glowCellOrder";
    UITableViewCell *cell = nil;
    if (cellIsLastOne == TRUE) {
        cell = [tableView dequeueReusableCellWithIdentifier:glowCellIdentifier];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }
     if (cell == nil) {
          if (cellIsLastOne == TRUE) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:glowCellIdentifier];  
             cell.selectionStyle = UITableViewCellSelectionStyleGray;
          } else {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
          }
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    if (cellIsLastOne == TRUE) {
        //name
        cell.textLabel.text = @"Bezahlt";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:25];
        cell.textLabel.textColor =  [UIColor whiteColor];
        //Glossy
        CALayer *thisLayer = cell.layer;
        if (thisLayer.sublayers.count == 1) {
            CAGradientLayer *glossLayer = [CAGradientLayer layer];
            glossLayer.frame = CGRectMake(cell.bounds.origin.x + cellOffset, cell.bounds.origin.y, self.tableView.frame.size.width - cellOffset * 2, cellItemHeight);
            glossLayer.colors = [NSArray arrayWithObjects:
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 (id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 nil];
            glossLayer.locations = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:0.0f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:1.0f],
                                    nil];
            [thisLayer addSublayer:glossLayer];
        }
    } else {       
        ItemID;
        cell.textLabel.text = @"Name";
        cell.detailTextLabel.text = @"Price";
        cell.detailTextLabel.textColor = [UIColor whiteColor];

        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.detailTextLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.textLabel.textColor =  [UIColor whiteColor];
    }
    cell.detailTextLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.detailTextLabel.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.shadowOffset = CGSizeMake(0, 2);
    cell.backgroundColor = [UIColor colorWithRed:0.72 green:0.76 blue:0.03 alpha:1.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    return cell;

此代码可以有效地重用单元并节省内存。

最新更新