我有一个带有默认背景颜色标题的tableview。我还有一个过滤条,用于过滤表格结果。过滤时,标题会变为全黑条。
如果我在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;
}
上面的代码通常会显示默认标题,搜索后会显示lightGray。如果我把[cell setBackgroundColor:nil];在if语句中,我得到了黑条。
有谁知道这里发生了什么?此外,如果没有优雅的修复,谁能告诉我默认的背景颜色是什么,所以我可以手动设置它?
感谢。
答案 0 :(得分:1)
当然,如果将背景颜色设置为nil
,它将变黑。您正在有效地摆脱单元格用于设置其背景颜色的UIColor
对象。
您必须将颜色设置回原来的颜色。我看到你上面有颜色代码,但你有评论。它不适合你吗?
cell.contentView.backgroundColor = [UIColor colorWithRed:0.663 green:0.663 blue:0.663 alpha:1];