UISearchDisplayController无法正确显示自定义单元格 [英] UISearchDisplayController not correctly displaying custom cells

查看:82
本文介绍了UISearchDisplayController无法正确显示自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含节和行的tableView,它使用自定义单元类。自定义单元格具有图像视图和一些标签。表视图工作正常,搜索工作,但搜索不显示我的自定义单元格类中的任何标签,只显示具有正确图像的imageView。我很困惑为什么会这样,特别是因为图像仍然显示,但不是标签。这是一些代码。

So I have a tableView that has sections and rows, and it uses a custom cell class. The custom cell has an image view and a few labels. The table view works fine, and the search works, except the search does not display any of the labels that are in my custom cell class, only the imageView with the correct image. I am quite confused as to why this is, especially since the image is still displayed, but not the labels. Here is some code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


//TODO: problem with search view controller not displaying labels for the cell, needs fixing
JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil) {
    cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

JSBook *book = nil;
//uses the appropriate array to pull the data from if a search has been performed
if(tableView == self.searchDisplayController.searchResultsTableView) {
    book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
else {
    book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}

FFMetaData *data = [self.ff metaDataForObj:book];
cell.titleLabel.text = book.title;
cell.priceLabel.text = [NSString stringWithFormat:@"$%@", book.price];
cell.authorLabel.text = book.author;
cell.descriptionLabel.text = book.description;

cell.dateLabel.text = [self.formatter stringFromDate:data.createdAt];
if(book.thumbnail == nil) {
    cell.imageView.image = [UIImage imageNamed:@"messages.png"];
    [self setCellImage:cell withBook:book atIndex:indexPath withTableView:tableView];
}
else {
    cell.imageView.image = [UIImage imageWithData:book.thumbnail];
}

return cell;

}

在此问题出现之前,我tableView中只有一个部分,一切都很完美。现在我有多个部分和行,按照我的描述,搜索被打破了。有任何想法吗?此外,对于 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 我曾经有 [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 但是现在如果我使用它,当我尝试搜索时会得到一个奇怪的异常:

Before this problem, I had only one section in the tableView, and everything worked perfectly. Now that I have multiple sections and rows the search is broken as I described. Any ideas? Also, for [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; I used to have [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; But now if I use that I get a weird exception when I try to search:

NSInternalInconsistencyException',原因:'请求无效索引路径处的rect (2个索引[1,1])'

NSInternalInconsistencyException', reason: 'request for rect at invalid index path ( 2 indexes [1, 1])'

所以这也让我感到困惑。感谢您的帮助!

So that is confusing me also. Thanks for the help!

推荐答案

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

不起作用,因为表格视图单元格已注册到特定的表格视图。这不适用于您的搜索结果控制器表视图。你确实找到了这个并切换到:

did not work because table view cells are registered to a specific table view. This will not work for your search results controller table view. You did find this out yourself and switched to:

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

这是正确的做法。

另外,在故事板中设计自定义单元格对于搜索结果控制器不起作用,因为您无法为主表视图设计搜索表视图的单元格。

Also, designing your custom cell in storyboard will not really work for your search results controller because you are not able to design cells for search table view, only for the main table view.

是的,你可以为你的搜索表视图注册该课程,就像你在这里一样,

Yes, you can register that class for your search table view, as you did here,

[self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];

但这不会包含您在故事板中自定义单元格中设计的任何内容。您必须以编程方式创建所有。

but that will not have any of the stuff you designed in your custom cell in storyboard. You would have to create all programmatically.

这篇关于UISearchDisplayController无法正确显示自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆