进入编辑模式时保持UITableViewCell背景颜色

Mas*_*son 4 objective-c uitableview ios

我为所有UITableViewCells设置了背景颜色.但是,当我单击我的UIBarButtonItem"编辑"时,删除和可拖动图标会扭曲背景颜色,使背后的白色背景.有没有办法解决?我可以在必要时显示代码,但这似乎是一个非常简单的问题.

小智 8

表格单元格的背景颜色通常设置如下:

cell.backgroundView.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,当创建UITableViewCell类的对象时,默认情况下它没有backgroundView,它是nil.开发人员需要在需要时创建backgroundView.因此,根据具体情况,您需要执行以下操作:

if (cell.backgroundView == nil) {
  cell.backgroundView = [[[UIView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)


Leg*_*las 7

这是uitableviewcells编辑模式中的默认行为.尝试再次为tableViewCells设置背景颜色

 - (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
  // set background color
}

else {
}
Run Code Online (Sandbox Code Playgroud)

如果需要,请尝试将背景颜色设置为您的属性,以便您可以在此处进行设置.

  • 谢谢!我没有意识到我必须分配/初始化我的后台视图,我认为这与tableViewCell一起提供.对我有用的代码是:`[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].backgroundView = [[[UIView alloc] init] autorelease]; [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].backgroundView.backgroundColor = [[cellArray objectAtIndex:i] color];`之后一切正常. (5认同)