我添加了搜索栏跟随教程,但由于某种原因,搜索不显示结果。
注意:我在第 2 行添加了“self tableView”以避免错误。也许这就是问题所在?还是IF的问题?
混帐:https ://github.com/dennis87/git_bookList
我认为问题出在这段代码中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell create
if (nil == cell)
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"Configuring cell to show search results");
Books *book = [[self searchResults]objectAtIndex:indexPath.row];
[[cell textLabel]setText:[book title]];
}
else
{
NSLog(@"Configuring cell to show normal data");
Books *book = [[self fetchResultsController]objectAtIndexPath:indexPath];
[[cell textLabel]setText:[book title]];
}
return cell;
}
我尝试放置 NSlog 并得到这个 NSLog 从未打印过,所以问题可能出在 IF 上?我做错了什么?
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSLog(@"Previous Search Results were removed.");
[self.searchResults removeAllObjects];
for (Books *book in [self.fetchResultsController fetchedObjects])
{
if ([scope isEqualToString:@"All"] || [book.title isEqualToString:scope])
{
NSLog(@"entered");
NSComparisonResult result = [book.title compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
NSLog(@"Adding book.title '%@' to searchResults as it begins with search text '%@'", book.title, searchText);
[self.searchResults addObject:book];
}
}
}
}