过去,我按照一个小教程在UITableView中实现了一个UISearchBar。
它是由数组组成的。现在我有.plist文件处理一切,searchBar怎么样? 我的意思是,我做了类似的事情:
copyLists = [[NSMutableArray alloc] initWithArray:sortedList copyItems:YES];
其中sortedList是包含plist的数组。
基本上,searchBar工作正常,但是当我输入一个单词时,即使我写出单元格的确切名称,它也不会显示任何单元格。
它应该返回plist中包含的密钥name
吗?
如何使用plist更改searchBar?
答案 0 :(得分:1)
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.filteredList removeAllObjects]; // First clear the filtered array.
NSArray *objcts = unfilteredList;
for (NSDictionary *section in objcts)
{
for (NSDictionary *row in [section valueForKey:@"Rows"])
{
NSRange rng = [[row valueForKey:@"name"] rangeOfString:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (rng.length != 0)
[self.filteredList addObject:row];
}
}
// Return YES to cause the search result table view to be reloaded.
return YES;
}
确保您的Search Bar and Search Display Controller
delegate
设置正确。然后显示单元格使用:
NSArray *rows;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
rows = filteredList;
} else {
NSDictionary *section = [unfilteredList objectAtIndex:indexPath.section];
rows = [section objectForKey:@"Rows"];
}
cell.textLabel.text = [[rows objectAtIndex:indexPath.row] objectForKey:@"name"];
并更改rowCount
,titleHeader
等