更改 iOS5 中所选 UITableViewCells 的背景颜色



我的应用在不同的屏幕中显示分组的静态UITableViews。无论如何使用外观代理

[UITableView appearance]

[UITableViewCell appearance]

要自定义所选单元格的背景颜色?基本上我需要修改

cell.selectedBackgroundView.backgroundColor

对于我的应用程序中的每个单元格,但我找不到要在代理对象中设置的正确属性。

顺便说一句,我也尝试了正常方法(作为组静态表):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];
}

但它不起作用。有什么建议吗?

您需要

为 selectedBackgroundView 提供一个视图。那里还没有。尝试:

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor] ;

此外,在创建单元格时,放置此代码(至少是第一行)的更好位置是-tableView:cellForRowAtIndexPath:。否则,每次显示单元格时,您都将创建新的背景视图。

最新更新