2013-12-09 50 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; 

此代碼可以有效地重用單元並節省內存。

相關問題