如何设置 UITableView 中单元格的背景颜色?
Thanks.
我知道这是一篇旧帖子,但我确信有些人仍在寻求帮助。您可以使用它来设置单个单元格的背景颜色,该颜色首先起作用:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
[cell setBackgroundColor:[UIColor lightGrayColor]];
然而,一旦你开始滚动,iPhone将重复使用单元格,这会混淆不同的背景颜色(如果你试图交替它们)。您需要调用 tableView:willDisplayCell:forRowAtIndexPath。这样,在加载重用标识符之前就设置了背景颜色。你可以这样做:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = ([indexPath row]%2)?[UIColor lightGrayColor]:[UIColor whiteColor];
}
最后一行只是一个精简的 if/else 语句。祝你好运!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)