简体   繁体   中英

Search bar results not showing in tableview

i've created a Search method which search through the content of the tableview. This works fine and every time i write in the search bar it adds the object to an array called filteredArray and shows the number of tableviewcells that are equal to filteredArray.count. i've tested this with NSLog(@"%@", filteredArray);

The problem is i want to show the filteredArray in the tableview when u search. i've changed the cellForRowAtIndexPath method for this, but it just gives me empty tableviewcells. What am i doing wrong?

Not searching:

在此处输入图片说明

searching "ru":

在此处输入图片说明

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    LanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

        if (tableView == self.tableViewData) {
    cell.patternLabel.text = [NSString stringWithFormat:@"%@", [[rows objectAtIndex:indexPath.row]objectAtIndex:0]];


        } else{
            cell.patternLabel.text = [NSString stringWithFormat:@"%@", [filteredArray objectAtIndex:indexPath.row]];

        }
    return cell;
}

search methods:

-(void) searchThroughdata {

    self.filteredArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@",self.searchBar.text];


    self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];

    NSLog(@"%@", filteredArray);



}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchThroughdata];
}

您可以在添加新字符后制作[yourTableView reloadData]

You have 2 table views, the cell is registered with one but not the other - this is the way prototype cells work when you define them in a storyboard which I guess you have done.

When the search table view doesn't get a dequeued cell you create one with initWithStyle:reuseIdentifier: but this doesn't create your patternLabel . So, you don't get a crash, because you return a cell, but the cell is empty because the label doesn't exist.

Your best options:

  1. Remove the prototype cell, create an XIB and register the NIB with each table view explicitly
  2. Not to use a search controller (so you only have 1 table view, manage the search bar yourself)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM