0

我正在开发一个应用程序,当我搜索其中一个单元格时,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];


} 

请帮助我

4

1 回答 1

0

你应该做什么:

在标题中:

@property(strong, nonatomic) NSMutableArray* displayItems;
@property(strong, nonatomic) NSArray* allItems;

在实施中:

- (void) viewDidLoad {
    ...
    self.addItems = [NSArray arrayWithObjects:@"1", @"2", @"3"];
    self.displayItems = [NSMutableArray array];
}

你的方法:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if ([searchText length] == 0) {
        [self.displayItems removeAllObjects];
        [self.displayItems addObjectsFromArray:allItems];

    } else {

        [self.displayItems removeAllObjects];
        for (NSString * string in self.allItems ){
        NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (r.location != NSNotFound){
            [self.displayItems addObject:string];
        }
    }

    [tableView reloadData];

} 

表视图数据源:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.displayItems count];
}


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }


    cell.textLabel.text = [self.displayItems objectAtIndex:indexPath.row];

    return cell;

}
于 2012-10-24T18:33:51.180 回答