我的搜索栏中有两个范围:城市和教堂。每次执行搜索时,两个范围都显示空白单元格,而它们确实有搜索结果。如何显示相应的单元名称?谢谢!
我认为问题出在我的cellForRowAtIndexPath
中if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchBar.scopeButtonTitles isEqual: @"City"]) {
cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Places"];
}
else if ([searchBar.scopeButtonTitles isEqual: @"Church"]) {
cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Church"];
}
}
else {
...
}
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
这是我的搜索:
- (void)filterListForSearchText:(NSString *)searchText scope:(NSString *)scope
{
[filteredResult removeAllObjects];
if ([scope isEqualToString: @"City"]) {
for (NSDictionary *dictionarySearch in allRegion) {
for (NSDictionary *citySearch in [dictionarySearch objectForKey: @"Cities"]) {
[citySearch objectForKey: @"Cities"];
NSRange rangeSearch = [[citySearch valueForKey: @"Places"] rangeOfString: searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (rangeSearch.location != NSNotFound) {
[filteredResult addObject: citySearch];
}
}
}
}
if ([scope isEqualToString: @"Church"]) {
for (NSDictionary *dictionarySearch in allRegion) {
for (NSDictionary *citySearch in [dictionarySearch objectForKey: @"Cities"]) {
for (NSDictionary *churchSearch in [citySearch objectForKey: @"Churches"]) {
[churchSearch objectForKey: @"Churches"];
NSRange rangeSearch = [[churchSearch valueForKey: @"Church"] rangeOfString: searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (rangeSearch.location != NSNotFound) {
[filteredResult addObject: churchSearch];
}
}
}
}
}
}
答案 0 :(得分:0)
希望This Example会帮助你。 只需在链接示例中将“if”和“else if”内容放在“filterContentForSearchText”中的“searchResults”中,这肯定会有效。
答案 1 :(得分:0)
我认为在搜索栏中使用NSMutableArray会对你有所帮助。您可以从链接获取示例项目。https://github.com/AalokParikh/MyDemoApps
答案 2 :(得分:0)
我找到了问题的答案。
的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (searchBar.selectedScopeButtonIndex == 0) {
cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Places"];
}
else if (searchBar.selectedScopeButtonIndex == 1) {
cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Church"];
}
}
else {
...
}
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
searchBar.selectedScopeButtonIndex == 0
适用范围城市,searchBar.selectedScopeButtonIndex == 1
适用范围教会