0

我有一个带有默认背景颜色标题的表格视图。我还有一个过滤栏,用于过滤表格结果。过滤时,标题变为全黑条。

如果我在 viewForHeaderInSection 中将背景颜色设置为某种特定颜色,则不会出现意外行为。如果我将 backgroundColor 设置为 nil,它会按预期工作,提供默认颜色,但在我搜索时不会。

不知何故,搜索结果的默认单元格背景颜色是黑色的?

这是我实验中的一些代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *cellIdentifier = @"HeaderCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    //cell.contentView.backgroundColor = [UIColor colorWithRed:0.663 green:0.663 blue:0.663 alpha:1];
    //[cell setBackgroundColor:[UIColor lightGrayColor]];
    [cell setBackgroundColor:nil];
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        [cell setBackgroundColor:[UIColor lightGrayColor]];
    }
    [[cell textLabel] setFont:[UIFont boldSystemFontOfSize:16]];
    [[cell textLabel] setTextAlignment:NSTextAlignmentCenter];
     cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

上面的代码将正常显示默认标题并在搜索后显示浅灰色。如果我把 [cell setBackgroundColor:nil]; 在 if 语句中,我得到了黑条。

有谁知道这里发生了什么?另外,如果没有优雅的修复,谁能告诉我默认背景颜色是什么,以便我可以手动设置它?

谢谢。

4

1 回答 1

1

好吧,如果您将背景颜色设置为 ,它当然会变黑nil。您正在有效地摆脱UIColor单元格用来设置其背景颜色的对象。

您必须将颜色设置回原来的颜色。我看到你有上面的颜色代码,但你有评论。它不适合你吗?

cell.contentView.backgroundColor = [UIColor colorWithRed:0.663 green:0.663 blue:0.663 alpha:1];
于 2013-08-20T22:11:18.827 回答