嗨,大家好
我的项目中有两个数组,像这样
- (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;
}
我正在使用此代码进行搜索..
#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 - 苹果
cell2-银河-三毛
...
当我搜索“htc”时,搜索工作正常,但是像这样的单元格
cell1- HTC One - 苹果
...
我能做些什么来解决这个问题?