Reputation: 354
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];
}
Upvotes: 0
Views: 1024
Reputation: 119031
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:
Upvotes: 0
Reputation: 887
you can make [yourTableView reloadData] after you add a new character
Upvotes: 1