2012-01-01 30 views
6

我试图更改当您滑动UITableViewCell行时出现的视图的背景颜色,这是'删除'按钮后面的背景颜色。在UITableViewCell中滑动以删除具有白色背景,需要清除

我试着改变cell.editingAccessoryView,但没做任何事情。

UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
    myBackgroundView.backgroundColor = [UIColor clearColor]; 
    cell.editingAccessoryView = myBackgroundView; 

任何想法?

+0

你有没有找到解决这个问题的方法?我有同样的问题:) – 2012-02-07 13:20:37

回答

7

你快到了。 UITableViewCell类具有backgroundView属性,默认情况下为nil。就像您在问题中所做的那样创建一个新的UIView,然后将其分配给您的单元格的backgroundView属性。

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
cell.backgroundView.backgroundColor = [UIColor clearColor]; 
1

我认为这取决于您如何将内容添加到您的单元格。

当我直接使用[cell addSubView]或[cell.contentView addSubView]将内容添加到单元格时,我遇到了同样的问题。

我的解决方法,这就是:

Create a view 
Add all your content(labels, images etc;) to this view 
Finally then add the view to your cell using [cell addSubView:tmpView] 

而且你不再需要用backgroundView财产篡改。我已经尝试过,并且完美无缺!

7

我在回答这个问题,因为我花了一段时间才找到答案,这是搜索中出现的第一个条目。通过使用willDisplayCell方法,您可以访问单元格背景颜色。请注意,[UIColor clearColor];将返回白色,请相应地调整您的代码。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.backgroundColor = [UIColor blackColor]; 

} 
0

虽然这些答案都是对的,我觉得对于大多数情况下,它更容易只是设置在界面生成器中的单元格背景颜色。我的意思是细胞的实际背景颜色属性,而不是它的内容视图。无论如何,如果它始终是内容视图的颜色,就没有理由动态执行此操作。