【问题标题】:UISearchDisplayController—why does my search result view contain empty cells?UISearchDisplayController——为什么我的搜索结果视图包含空单元格?
【发布时间】:2012-03-18 20:44:54
【问题描述】:

我快要发疯了,在我的核心数据数据库中,我有很多用户,我已经通过 NSFetchedResultController 将数据库连接到 tableviewcontroller,当视图加载时,我会看到所有用户,我可以通过storyboard segue对细节视图控制器执行推送......到目前为止,上帝,我的tableview包含一个带有图像视图和2个uitextlabels的自定义单元格,它们都分配了它们的标签值..

现在,当我执行搜索时,我创建一个带有谓词的新 NSFetchedResultController,并执行 performFetch.. 然后我获取 fetchedObjects 属性,它包含匹配的用户,因此搜索请求也像魅力一样工作。 . 在我所有的 tableviews 数据源中,我检查 self.tableView == self.searchdispl.view 以查看我应该从哪个控制器查询数据.. 在所有控制器委托中,我检查哪个控制器处于活动状态,因此我知道要重新加载哪个视图.如果我 nslog 大量数据,在幕后一切都很好,委托和数据源方法都使用正确的视图和 fetchcontroller..

在我的 cellForRowAtIndexPath 中,我可以注销我的用户名,这始终是正确的,在搜索时也是如此。

问题是顶部的 UISearchDisplayController 视图只有空单元格,除非我将 UITableViewControllers 动态原型单元格设置为基本单元格,否则两个表格视图都包含带有用户名的标签!单元格中的segue仍然不起作用,但单元格中有数据。

UISearchDisplayControllers 视图似乎是一个通用视图,至少在涉及到 segues 和单元格布局时...

我已经检查了从 UISearchDisplayController 到我的 UITableViewController 的所有连接,一切看起来都很好......

为什么 SearchDisplayControllers 视图不接受我的单元格布局和 segue ?

谢谢你:)

更新:刚刚弄清楚出了什么问题,但还没有找到解决方案。 在我的 cellForRowAtIndexPath 中,当 searchdisplayview 在此方法中时,单元格始终配置为默认单元格。我真的需要在代码中创建单元格以使 uisearchdisplay 工作吗?为什么它不能使用故事板中配置的单元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"PersonCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSLog(@"Default Cell");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSFetchedResultsController *controller = tableView == self.tableView ? self.fetchedResultsController : self.fetchedResultsControllerForSearch;

Person *p;
p = [controller objectAtIndexPath:indexPath];       

[(UILabel*) [cell viewWithTag:1] setText:p.name]; 
[(UILabel*) [cell viewWithTag:2] setText:@"Status"];    

if(p.avatarImage){
    UIImage *avatar = [[UIImage alloc] initWithData:p.avatarImage];
    [(UIImageView *) [cell viewWithTag:3] setImage:avatar];      
}

return cell;

}

【问题讨论】:

  • 好吧,我就让它坐在这里,即使我刚刚弄清楚出了什么问题,我有点尴尬,但也许这篇文章可以帮助其他人:) 问题是当我得到cellforrowatindexpath 中的单元格,它不是从原始 viewcontrollers tableview 中获取的,所以我将行从 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];到 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];,现在一切都很好,单元格具有故事板的布局,并且 segues 就像一个魅力:)
  • 这确实有效,您应该将其发布为您自己问题的答案!

标签: iphone nsfetchedresultscontroller uisearchdisplaycontroller


【解决方案1】:

问题是当我在 cellforrowatindexpath 中获取单元格时,它没有从原始 viewcontrollers tableview 中获取它,所以我从

更改了行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

现在一切都很好,单元格具有故事板中的布局,并且 segues 像魅力一样工作..

【讨论】:

  • 这解决了我的问题。我在 iOS 8 上尝试这个,感觉就像一个可怕的黑客。悲伤的脸...
猜你喜欢
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多