如何更改背景选择的颜色故事板静态单元格

jca*_*cho 6 storyboard uitableview

我有一个带故事板的应用程序.在一个场景中,我有一个带有静态单元格内容的tableview.是否可以将背景选择的颜色更改为默认选项(蓝色和灰色)中的另一种颜色?

我知道如果我可以在forRowAtIndexPath中更改单元格背景颜色,但在这种情况下我没有tableview中的任何数据源函数.我确信可以通过IB或其他功能修改...

提前致谢!

Lea*_*ves 18

您不需要编写任何一行代码来完成此操作.您可以使用故事板完成所有操作.就这样做:

  1. 一个添加UIView到您的UITableViewCell并将其链接到该selectedBackgroundView小区物业(找到此属性,则需要从拖行"New Reference Outlet",并释放在所需UITableViewCell)
  2. 将颜色更改UIView为所选状态的所需颜色

您可以对backgroundView酒店做同样的事情.此外,您可以使用a UIImageView来使用图像,而不是a的单色背景UIView.

这是一个使用自定义UIViewController而不是UITableViewController的示例文件,但它适用于:http://uxp.com.br/downloads/CustomCell.zip

  • 如何在不收到此错误的情况下完成此操作?http://stackoverflow.com/questions/13090338/runtime-error-setting-backroundview-property-of-uitableviewcell-in-storyboard (2认同)

Car*_*los 9

我有同样的问题.解决方案有两个部分:

1)得到细胞,看看这个.2)更改背景颜色:您必须使用所需的背景颜色创建UIView,并将其设置为单元格的selectedBackgroundView

例如,我在uitableviewcontroller的viewDidLoad中使用了这段代码:

UIView *backgroundSelectedCell = [[UIView alloc] init];
[backgroundSelectedCell setBackgroundColor:[UIColor colorWithRed:130/256.0 green:169/256.0 blue:171/256.0 alpha:1.0]];

for (int section = 0; section < [self.tableView numberOfSections]; section++)
    for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
    {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];

        [cell setSelectedBackgroundView:backgroundSelectedCell];
    }  
Run Code Online (Sandbox Code Playgroud)