我試着瞭解從一些教程發生了什麼,但不幸的是我沒有找到答案。 我認爲這個問題可能是我沒有IF萬一我在搜索結果中爲零。UIsearchBar顯示結果,但當我按下的東西,沒有存在的應用程序粉碎
錯誤是: 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引0超出界限爲空數組'
我使用的方法:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//removes previos search result
[[self searchResults]removeAllObjects];
for (Books *book in [self.fetchResultsController fetchedObjects])
{
if([self searchResults] != nil)
{
NSComparisonResult result = [book.title compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.searchResults addObject:book];
}
}
}
}
還可以在GIT中找到我的全部項目: https://github.com/dennis87/git_bookList
編輯: 當我嘗試把這個像這樣
if([scope isEqualToString:@「All」] || [book.title isEqualToString:範圍])
應用美眉沒有讓我按任何東西..
謝謝:)在numberOfSectionsInTableView 我試試這個: 如果([個體經營SearchResult所]) 返回1; else return 0; 在titleForHeaderInSection我試試這個: 如果((的tableView == [自searchDisplayController] searchResultsTableView])&& [個體經營SearchResult所]) 但它不工作。我如何檢查數組是否爲空?如你所知,我是obj c的新手。 – Dennis
@ Dennis:使用if([[self searchResults] count] == 0)'來檢查數組是否爲空。 –
感謝它的工作:) – Dennis