1

我有一个奇怪的问题UISearchDisplayController

我的代码如下:

numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if ([[self searchDisplayController] isActive])
  {
    return [self.itemsFound count];

  } else {
    return [self.items count];
  }

  return [self.items count];
}

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  FavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

  Facilities *f = nil;

  if ([[self searchDisplayController] isActive])
  {
    f = [self.itemsFound objectAtIndex:indexPath.row];

  } else {
    f = [self.items objectAtIndex:indexPath.row];
  }

  [cell.favouriteButton addTarget:self action:@selector(toggleFavourite:) forControlEvents:UIControlEventTouchUpInside];
  [cell.titleLabel setText:f.name];

  return cell;
}

下一个:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
  NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchText];

  self.itemsFound              = [self.items filteredArrayUsingPredicate:resultPredicate];
}

shouldReloadTableForSearchString:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

  self.itemsFound = nil;

  [self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:  [self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];
  return YES;
}

我的桌子:

桌子

如果我输入列表中的字母:

输入字母 a 后

结果存在但未显示在列表中。

在下一张图片上没有结果,屏幕显示正确的信息。

没有结果

我不知道为什么我不能在第二个屏幕上显示结果,而结果(显示在控制台上)。

你有什么主意吗 ?

谢谢你的时间。

4

0 回答 0