无法更改UITableViewCell背景颜色

Yos*_*far 5 iphone xcode objective-c uitableview ios

我有UITableViewCell适合的UITableView,但不知何故我进入编辑风格模式时无法改变背景颜色..我已经尝试了网上的所有内容,整天搜索,但仍然无法修复它(我已经搜索过了) StackOverflow也是如此).请帮我.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (MainTableViewCell *)view;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ale*_*ciu 18

尝试自定义tableView: willDisplayCell:方法中的单元格,如下所示:

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

    cell.backgroundColor = [UIColor redColor];

}
Run Code Online (Sandbox Code Playgroud)


Wes*_*ley 5

您需要更改tableView单元格的contentView,而不是单元格的背景视图。

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

    cell.contentView.backgroundColor = [UIColor redColor];
}
Run Code Online (Sandbox Code Playgroud)