commitEditingStyle的透明背景

时间:2012-01-17 03:46:38

标签: iphone objective-c ios uitableview

在滑动第三个单元格后出现删除按钮时,背景将被剪裁。我怎样才能解决这个问题?这是制作自定义单元格时的代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    
      (NSIndexPath *)indexPath
     {
         static NSString *CellIdentifier = @"customCell";

         BookMarksCustomCell *cell = (BookMarksCustomCell *)[tableView  
         dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            NSArray * topLevelObjects = [[NSBundle mainBundle]  
            loadNibNamed:@"BookMarksCustomCell" owner:self options:nil];

        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (BookMarksCustomCell *)currentObject;
                break;
            }
         }        
    }

    .....more logic stuff.

    //alternating cell background.
    if([indexPath row] % 2 == 0)
        cell.contentView.backgroundColor = [UIColor colorWithRed:234.0/255.0 
        green:234.0/255.0 blue:234.0/255.0 alpha:1.0];

    ......
   }

http://i228.photobucket.com/albums/ee262/romano2717/photo3.png

1 个答案:

答案 0 :(得分:3)

您的代码正在修改单元格的contentView属性的背景颜色。当删除按钮出现时,此视图会调整大小,因此您需要设置单元格本身的背景颜色(也是UIView子类)。这应该解决它。

此外,考虑到单元格重用和更改索引,无论当前单元格是偶数还是奇数,都应设置背景颜色。始终明确地将颜色设置为某些东西(即使它是白色的),这样当您滚动时,不会对重复使用的单元格产生奇怪的效果。

我也在UITableViewCell文档中注意到了这个注释:

  

如果要更改单元格的背景颜色(通过UIView声明的backgroundColor属性设置单元格的背景颜色),必须在tableView:willDisplayCell:forRowAtIndexPath:委托方法中执行此操作,而不是tableView:cellForRowAtIndexPath:数据源。

所以,请在那里做。