ios - 搜索后自定义 UITableViewCell View 为空白

标签 ios objective-c uitableview uisearchdisplaycontroller

我相信这个问题是我的确切问题,但我无法通过查看接受的答案来解决问题。
UISearchBar: FilterContentForSearchText not working on a UITableView (results not shown on table)

我有一个 UITableViewController允许搜索。它通过默认 UITableViewCellStyle 完美运行,然后我决定为单元格实现我自己的自定义布局。我没有继承 UITableViewCell ,而是添加了两个 UILabel s 通过 Interface Builder 到单元格并设置自动布局。我为它们分配了唯一的标签,以便我可以在 cellForRowAtIndexPath: 中引用这些标签.它工作得很好 - 一切都按预期显示,直到您搜索。搜索确实有效,它显示了部分和行,但行上的标签没有文本,因此单元格显示为完全空白。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"List View Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UILabel *leftLabel = (UILabel *)[cell viewWithTag:100];
    UILabel *rightLabel = (UILabel *)[cell viewWithTag:101];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        leftLabel.text = ...;
        rightLabel.text = ...;
    }
    else {
        leftLabel.text = ...;
        rightLabel.text = ...;
    }
    return cell;
}

我知道为什么标签是空白的。当您搜索时,dequeueReusableCellWithIdentifier:返回 nil因此它使用重用标识符初始化一个新单元格,但该单元格没有 viewWithTag:这导致左右标签变为nil .所以它显然不能将文本添加到 nil .

链接中提供的答案表明您应该在 if (!cell) 中创建标签声明,然后调用[cell.contentView addSubview:left{right}Label]; .我这样做了,然后将我的标签配置代码也移到了那个 if 语句中。但是当我这样做时,主表的行只有我在 Storyboard 中的左右标签的默认值 - 它不会设置标签的文本。这是因为 dequeueReusableCellWithIdentifier:不返回 nil而是创建一个新单元格,因此它不会设置文本,因为它位于 if (!cell) 中陈述。

我不知道如何处理这两种情况:当 cell 为 nil 时,何时不是。我需要做什么来解决这个问题?

更多评论:我以前从未使用过 xib 文件,我更愿意保持这种状态。 :) 我不介意继承 UITableViewCell如果这是一个解决方案。当然,我想以“正确”的方式实现这一点 - 仅在需要时创建一个单元格等。

最佳答案

我认为最简单的方法是在 xib 文件中创建单元格,如果您想对主表和搜索结果表使用相同的单元格类型。如果需要,您可以创建一个子类(您只需将 IBOutlets 放入 .h 文件中的两个标签中),或者按照您已经使用标签的方式进行操作。在 TableView Controller 的 viewDidLoad 中,为两个表注册 nib,

[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CommonCell" bundle:nil] forCellReuseIdentifier:@"CommonCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"CommonCell" bundle:nil] forCellReuseIdentifier:@"CommonCell"];

然后在 cellForRowAtIndexPath: 中,您只需将具有相同标识符的单元格出列,并填充标签。没有必要检查单元格是否等于 nil,因为它永远不会。

我修改了我的一个应用程序来展示如何实现 cellForRowAtIndexPath。我对单元格进行了子类化(CommonCell 是类),只将 IBOutlets 添加到 leftLabel 和 rightLabel,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommonCell" forIndexPath:indexPath];
    cell.leftLabel.text = ([tableView isEqual:self.tableView])? self.theData[indexPath.row] : self.filteredData[indexPath.row];
    cell.rightLabel.text = ([tableView isEqual:self.tableView])? self.theData[indexPath.row] : self.filteredData[indexPath.row];
    return cell;
}

关于ios - 搜索后自定义 UITableViewCell View 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398567/

相关文章:

ios - NSInvalidArgumentException 有一个字典

iphone - if else 语句混淆 Objective-C

ios - 突出显示 uitableViewCell

objective-c - 在编辑模式下将 UISearchBar 与 UITableView 一起使用时,搜索结果不处于编辑模式

ios - iOS 上的 Cordova 应用程序中的 Iframe Youtube 视频不再工作

objective-c - 一个类别可以同时实现一个协议(protocol)吗?

iphone - 无法删除 View 的 subview

ios - 在 Swift 的 UITableViewCell 中设置图像

ios - NSFetchedResultsController.performFetch() 上的 EXC_BREAKPOINT 因为 -[CFString isNSString__] : message sent to deallocated instance

ios - iOS 7.1 颜色变化,如何改变搜索栏颜色?