【问题标题】:Changing background color of selected UITableViewCells in iOS5在 iOS5 中更改选定 UITableViewCells 的背景颜色
【发布时间】:2012-09-11 16:44:45
【问题描述】:

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

[UITableView appearance]

[UITableViewCell appearance]

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

cell.selectedBackgroundView.backgroundColor

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

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

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

    cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

}

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

【问题讨论】:

    标签: uitableview background background-color ios5.1 uiappearance


    【解决方案1】:

    您需要为 selectedBackgroundView 提供一个视图。那里已经没有了。试试:

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

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

    【讨论】:

    • -tableView:cellForRowAtIndexPath: 影响所有单元格。
    • 我的意思是,选定的背景视图部分会改变选定单元格的颜色。但是,当您拖动时,单元格本身会取消选择。另外请注意,使用willDisplayCell 委托方法在单元格中放置任何类型的预视觉效果是一种很好的做法。
    猜你喜欢
    • 2012-04-30
    • 2012-03-14
    • 2011-08-14
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多