search后,TableView中的单元格的值不正确

嗨,大家好

我在我的项目中有这样的数组

- (void)viewDidLoad { [super viewDidLoad]; deviceName = [[NSArray alloc] initWithObjects: @"iPhone", @"Galaxy", @"iPad", @"iMac", @"HTC One", nil]; companyName = [[NSArray alloc] initWithObjects:@"Apple", @"Samsung", @"Apple", @"Apple", @"HTC", nil]; } 

我正在使用此代码来修改单元格..

 #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.searchDisplayController.searchResultsTableView) { return [searchResult count]; } else { return [deviceName count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; } if ([self.searchDisplayController isActive]) { NSString *name = [searchResult objectAtIndex:indexPath.row]; NSString *company = [companyName objectAtIndex:indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", name, company]; } else { NSString *name = [deviceName objectAtIndex:indexPath.row]; NSString *company = [companyName objectAtIndex:indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", name, company]; } return cell; } 

我正在使用此代码来search..

 #pragma mark - UISearchDisplayController delegate methods - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; [searchResult release]; searchResult = [[deviceName filteredArrayUsingPredicate:resultPredicate]retain]; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } 

开始的时候细胞就可以这样

cell1- iPhone – 苹果

细胞2 – 银河 – Samaung

当我search'HTC'search工作正常,但这样的细胞

HTC One – 苹果

我能做些什么来解决这个问题?

您有两个单独的数组作为表视图数据源:

 deviceName = [ "iPhone", "Galaxy", "iPad", "iMac", "HTC One"] companyName = [ "Apple", "Samsung", "Apple", "Apple", "HTC" ] 

然后你在你的例子中从deviceName创build一个过滤的数组

 searchResult = [ "HTC One"] 

cellForRowAtIndexPath您使用已过滤的数组和原始数组companyName ,这就是为什么您要显示“HTC One – Apple”。

为了解决这个问题,你不应该使用两个单独的数组作为数据源,而是使用单个字典数组 ,其中每个字典都包含设备名称和公司名称:

 allDevices = @[ @{@"name": @"iPhone", @"company": @"Apple"}, @{@"name": @"Galaxy", @"company": @"Samsung"}, ... ]; 

你会像这样过滤数组:

 NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchText]; filteredDevices = [allDevices filteredArrayUsingPredicate:resultPredicate]; 

所以filteredDevices也是一个字典数组,包含每个设备的名称公司。 然后,在cellForRowAtIndexPath ,你可以简单地做

 NSDictionary *device; if ([self.searchDisplayController isActive]) { device = filteredDevices[indexPath.row]; } else { device = allDevices[indexPath.row]; } NSString *name = device[@"name"]; NSString *company = device[@"company"]; cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", name, company]; 

备注:我省略了所有保留/发布的电话,因为我通常与ARC合作。