- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
// cell.backgroundColor = [UIColor blueColor];
cell.contentView.backgroundColor = [UIColor blueColor];
return cell;
}
cell.backgroundColor = [UIColor blueColor];
<span style="color:#ff6666;"> //cell本身就是个UIView,如果直接改cell的backgroundColor 会被contentview覆盖掉</span>
cell.contentView.backgroundColor = [UIColor blueColor];
但是如果到了编辑模式就会出现以下情况= =。是不是太丑了……
Cocoa提供的按钮背景色为透明。
因为ContentView被移开,下面是tableView的颜色,已经不是cell的一部分了
解决方案:
所以,最好的方式应该是通过cell.backgroundView来改变cell的背景。
按照文档说明,backgroundView始终处于cell的最下层,所以,将cell里的其它subview背景设为[UIColor clearColor],以cell.backgroundView作为统一的背景,应该是最好的方式
代码如下:
//把cell的图片背景改了 backgroundView ~~~~
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_bg"]];
cell.backgroundView = imageV;
</span>
效果:
是不是很可爱~~~很和谐~~~~