UISearchDisplayController不正确显示自定义单元格

所以我有一个tableView有部分和行,它使用自定义单元类。 自定义单元格有一个图像视图和一些标签。 表视图工作正常,search工作,除了search不显示任何在我的自定义单元类中的标签,只有imageView与正确的图像。 我很困惑,为什么这是,尤其是因为图像仍然显示,而不是标签。 这是一些代码。

- (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中只有一个部分,一切都很完美。 现在我有了多个部分和行,search就像我所描述的那样被破坏了。 有任何想法吗? 另外,对于[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 我曾经有[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 但是现在,如果我使用它,当我尝试search时,会出现一个奇怪的exception:

NSInternalInconsistencyException',原因:'请求rect在无效索引path(2个索引[1,1])'

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

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

没有工作,因为表视图单元格注册到特定的表视图。 这不适用于您的search结果控制器表视图。 你确实发现了这一点,并切换到:

 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

这是正确的做法。

此外,在故事板中devise自定义单元格对于search结果控制器来说不会真正起作用,因为您无法为search表视图devise单元格,只能为主表视图devise单元格。

是的,您可以像在此处那样为您的search表格视图注册该课程,

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

但是在故事板中的自定义单元中没有devise任何东西。 你将不得不以编程方式创build。

当我尝试search时,我遇到了同样的exception,以及如何解决这个问题。

  if (tableView == self.searchDisplayController.searchResultsTableView) { cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; }else{ cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; } 

UITableView与search栏和search显示使用故事板原型中devise的相同的自定义单元格的总结:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifierAsYouDefinedInStoryboard"; CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (tableView == self.searchDisplayController.searchResultsTableView) { /* searchResultsTableView code here */ } else { /* Base tableView table code here */ } /* more cell code here */ return cell; } 

然后为searchResultsTableView添加此行以匹配您的自定义单元格高度:

 - (void)viewDidLoad { [super viewDidLoad]; [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight]; /* more of your viewDidLoad code */ } 

如果您在UIViewController中使用UITableView,并且想要在StoryBoard中为您的searchDisplayController重用您创build的单元标识符,请尝试以下操作:

StoryBoard> UIViewController> Reference Outlets>将tableView链接到你的UIViewController的.h文件,并把它称为tableView,所以你应该有这样的东西:

 @property (strong, nonatomic) IBOutlet UITableView *tableView; 

所以不要这样做:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] } 

做这个

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] } 

如果你在故事板上创build一个单元格,你不应该注册这个类(这实际上是把事情搞砸了)。 如果您使用代码创build单元格,则注册该类;如果将该单元格放入笔尖,则注册一个笔尖。 如果在故事板中创build它,则不会注册任何内容,而是使用dequeueReusableCellWithIdentifier:forIndexPath:,而且根本不需要if(cell == nil)子句。

试试这个,这对我有用

 JSBookCell * cell = [yourtableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];