UISearchbar和UITableViewCell

时间:2012-10-24 17:58:23

标签: ios uitableview uisearchbar

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


} 

请帮助我

1 个答案:

答案 0 :(得分: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;

}