我正在尝试过滤4个阵列,但我无法使其工作。这是一个使用NSString填充的推送表视图。当我在搜索栏中输入内容时,搜索结果来自1个数组(MARLINS),即使在其他数组中也是如此。我知道问题出在-(void)filtertContentForSearchText:
。我错过了什么?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults2 count];
}
else
{
if ([testName isEqualToString:@"CES"])
return [self.CES count];
if ([testName isEqualToString:@"MARLINS"])
return [self.MARLINS count];
if ([testName isEqualToString:@"DELTA 2, NAVIGATOR"])
return [self.DELTA count];
if ([testName isEqualToString:@"CREW 2002, NAVIGATOR"])
return [self.CREW count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifire = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifire];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifire];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResults2 objectAtIndex:indexPath.row];
}
else
{
if ([testName isEqualToString:@"CES"]) {
cell.textLabel.text = [self.CES objectAtIndex:indexPath.row];
}
else if ([testName isEqualToString:@"MARLINS"]) {
cell.textLabel.text = [self.MARLINS objectAtIndex:indexPath.row];
}
else if ([testName isEqualToString:@"DELTA 2, NAVIGATOR"]) {
cell.textLabel.text = [self.DELTA objectAtIndex:indexPath.row];
}
else if ([testName isEqualToString:@"CREW 2002, NAVIGATOR"]) {
cell.textLabel.text = [self.CREW objectAtIndex:indexPath.row];
}
}
return cell;
}
#pragma Search Methods
-(void)filtertContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
self.searchResults2 = [ self.MARLINS filteredArrayUsingPredicate:predicate];
self.searchResults2 = [ self.CES filteredArrayUsingPredicate:predicate];
self.searchResults2 = [ self.CREW filteredArrayUsingPredicate:predicate];
self.searchResults2 = [ self.DELTA filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filtertContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
@end
答案 0 :(得分:0)
(从评论中提取到'答案',稍微编辑以便更好地合作):
您有在filter方法中未使用的scope参数。从shouldReloadTableForSearchString传递一些有用的东西:知道你应该过滤哪个数组。
查看范围变量。如果它的现有值可用于标识要使用的数组,那么至少可以复制if ... else块,类似于cellForIndexPath中的代码,只过滤特定的数组。 / p>