I have got an application which has a UITableView
containing two sections. Now I wanted to add a UISearchBar
to allow searching. And the searching really works pretty fine and selects the correct cells. When searching you can see that there are cells you can select, but the labels are blank. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"qrCodeCell";
QRTableViewCell *cell = (QRTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[QRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
if (tableView == self.searchDisplayController.searchResultsTableView) {
//NSLog(@"%@",[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]);
[cell.dataLabel setText:[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]];
//NSLog(@"%@",cell.dataLabel.text);
NSDate *timeStamp = [[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
[cell.timeLabel setText:[format stringFromDate:timeStamp]];
} else {
[cell.dataLabel setText:[[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
NSDate *timeStamp = [[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
[cell.timeLabel setText:[format stringFromDate:timeStamp]];
}
} else if (indexPath.section == 1) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
if (tableView == self.searchDisplayController.searchResultsTableView) {
[cell.dataLabel setText:[[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"data"]];
NSDate *timeStamp = [[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
[cell.timeLabel setText:[format stringFromDate:timeStamp]];
} else {
[cell.dataLabel setText:[[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
NSDate *timeStamp = [[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
[cell.timeLabel setText:[format stringFromDate:timeStamp]];
}
}
return cell;
}
This is pretty much exactly the same issue as in these questions, but the problem could not be resolved: UISearchBar with UITableView containing custom cells => blank cells and It doesn't show the contents of the cells in a UITableView filtered by a UISearchDisplayController
I would really appreciate if someone can help me!
EDIT:
Here is some more code from my TableViewController
:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"data contains[c] %@", searchText];
self.searchResultsQR = [self.qrcodes filteredArrayUsingPredicate:resultPredicate];
self.searchResultsScanned = [self.scannedCodes filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}