我正在开发一个应用程序,当我搜索其中一个单元格时,UItableview 代码出现问题,例如:我想进入单元格 60,我在搜索栏中搜索它,但它没有给我 cell60 它是给我表格中的第一个单元格 我该如何修复它
AllItems 是 NSArray 并且 displayItems 是 NSMutableArray
这是代码
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Crete an NSSring object that we can use as the eruse identifir
static NSString *CellIdentifier = @"Cell";
//Check to see if wer can reuse a cell from a row that has just roolerd off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
//if there are no cells to be reused creater new cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//set the text attribute to whatever we are currently looking at in our array
//Return the cell
return cell;
}
这是搜索栏的代码
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:allItems];
} else {
//here
[displayItems removeAllObjects];
for (NSString * string in allItems){
NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound){
[displayItems addObject:string];
}
}
}
[tableView reloadData];
}
请帮助我