转载自:http://haoxiang.org/2010/12/uitableviewcell-background/
自己总结下吧:其实就一句话:把cell里的变量类都设成clearcolor,然后在这样来设置cell的主背景:
- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...................
UIView *backgrdView = [[UIView alloc] initWithFrame:cell.frame];
backgrdView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GcBg.png"]];
cell.backgroundView = backgrdView;
[backgrdView release];
return cell;
}
tableView.backgroundColor = [UIColor greenColor]改变整个tableView的颜色,对于tableViewCell,我们不应该直接使用cell.backgroundColor。Cell本身是一个UIView,我们所看到的部分其实只是它的一个Subview,也就是cell.contentView。所以,如果直接改变cell本身的背景色,依然会被cell.contentView给覆盖,没有效果。
cell.contentView.backgroundColor = [UIColor blueColor];
Cocoa提供的按钮背景色为透明。因为ContentView被移开,下面是tableView的颜色,已经不是cell的一部分了。
所以,最好的方式应该是通过cell.backgroundView来改变cell的背景。按照文档说明,backgroundView始终处于cell的最下层,所以,将cell里的其它subview背景设为[UIColor clearColor],以cell.backgroundView作为统一的背景,应该是最好的方式。
Saved Blog
UITableViewCell是一个很常用的View,通常我们都是直接使用它。
- (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.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
return cell;
} |
得到这个效果:

现在我们给tableViewCell加上点背景色:
- (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.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
// cell.backgroundColor = [UIColor blueColor];
cell.contentView.backgroundColor = [UIColor blueColor];
return cell;
} |
我们不应该直接使用cell.backgroundColor。Cell本身是一个UIView,我们所看到的部分其实只是它的一个Subview,也就是cell.contentView。所以,如果直接改变cell本身的背景色,依然会被cell.contentView给覆盖,没有效果。

不过,通过cell.contentView.backgroundColor来改变背景色还不是最好的Practice. 如果通过
进入Edit模式,就会出现问题。

Cocoa提供的按钮背景色为透明。因为ContentView被移开,下面是tableView的颜色,已经不是cell的一部分了。
所以,最好的方式应该是通过cell.backgroundView来改变cell的背景。按照文档说明,backgroundView始终处于cell的最下层,所以,将cell里的其它subview背景设为[UIColor clearColor],以cell.backgroundView作为统一的背景,应该是最好的方式。
- (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.textLabel.text = [NSString stringWithFormat:@"Line: %d", indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgrdView = [[UIView alloc] initWithFrame:cell.frame];
backgrdView.backgroundColor = [UIColor blueColor];
cell.backgroundView = backgrdView;
[backgrdView release];
return cell;
} |
效果:


This entry was posted in
iOS Development,
文章 and tagged
cocoa,
技巧,
移动开发 by
Haoxiang Li. Bookmark the
permalink.