【发布时间】:2013-08-23 03:05:31
【问题描述】:
我有一个带有默认背景颜色标题的表格视图。我还有一个过滤栏,用于过滤表格结果。过滤时,标题变为全黑条。
如果我在 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 语句中,我得到了黑条。
有人知道这里发生了什么吗?另外,如果没有优雅的修复,谁能告诉我默认背景颜色是什么,以便我可以手动设置它?
谢谢。
【问题讨论】:
-
你在哪里设置标题的默认背景颜色?
-
[cell setBackgroundColor:nil] if 语句之间。在上面的代码中,我展示了如何使它工作,排序,在搜索中没有默认颜色。如果我删除我提到的行下方的 if 语句,那么我会在搜索结果中看到黑条行为。
-
如果你删除这行
[cell setBackgroundColor:nil]; if(tableView == self.searchDisplayController.searchResultsTableView) { [cell setBackgroundColor:[UIColor lightGrayColor]]; }会发生什么。我的意思是你为什么要在这种方法上将背景设置为零?我猜你是在 Interface Builder 上为这个单元格分配背景颜色;但是在这段代码中,您只调用了一个自定义单元格: "HeaderCell" 所以所有标题都具有相同的背景颜色?为什么不在这里也通过代码应用它 -
如果您有多种背景颜色,那么您应该在 Interface Builder 上使用不止一个自定义单元格。或者您可以根据其部分通过代码分配背景颜色,但此逻辑也应出现在您的方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中 -
完全不应用它会导致黑条行为。这与始终设置 backgroundcolor = nil 相同。我明确设置它是因为我认为搜索结果可能是从其他地方或其他地方提取背景颜色。
标签: ios objective-c uitableview default background-color