2013-12-09 45 views
0

我有一个问题,最好的方式来描述它是向你展示图片。 该部分的最后一行应该有光泽风格和文本对齐中心。 首先我有一个表,这样的一个部分: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; 

我真的没有得到的问题。请帮帮我。谢谢! ;)

+0

我可以看到生成'''cellIsLastOne'''的代码吗? –

回答

0

那么,你的屏幕截图显示的问题表明你的计算cellIsLastOne是错误的。

但即使它是正确的,当您滚动浏览表格视图时,这会产生很多问题,因为单元格正在被重新使用。要解决,您可以:

  • 使用不同的再利用标识符光泽细胞
  • else块取出光泽子层

基本上,当你调用dequeueReusableCellWithIdentifier:,它可能返回您之前添加了光面图层的单元格。此外,这可能会多次添加光面图层。

+0

感谢您的建议!但即使当我将标识符更改为这样一个唯一的字符串:NSString * CellIdentifier = [NSString stringWithFormat:@“Cell%d%d”,indexPath.row,indexPath.section];它不工作:/ – perotom

+0

@ user2630406你不想这样做;那么你永远不会再使用任何单元。你所描述的问题与'cellIsLastOne'有关。如果您的表格视图增大到足以滚动,那么这将解决您稍后会遇到的另一个问题。 –

0

我编辑你的代码可以帮助你。

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; 

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

相关问题