0

我的应用程序在不同屏幕中显示分组静态UITableViews。反正是有使用外观代理更改iOS5中所选UITableViewCells的背景颜色

[UITableView appearance] 

[UITableViewCell appearance] 

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

cell.selectedBackgroundView.backgroundColor 

在我的应用程序的每个细胞,但我不能找到合适的物业在代理对象进行设置。

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

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor]; 

} 

,但它不工作。任何建议?

回答

7

您需要提供selectedBackgroundView的视图。那里已经没有人了。尝试:

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

另外,在创建单元时,一个更好的地方把这个代码(至少是第一行)是-tableView:cellForRowAtIndexPath:。否则,每次显示单元格时都会创建一个新的背景视图。

+0

如果你拖动并选择其他单元?这似乎并不正确的答案。 –

+0

-tableView:cellForRowAtIndexPath:影响所有单元格。 – leftspin

+0

我的意思是,选定的背景视图部分将改变选定的单元格的颜色。但是,单元格本身会在您拖动时取消选择。对于你来说,最好的做法是用'willDisplayCell'委托方法在单元格中加入任何类型的预视效果。 –