我有一個包含兩個部分的UITableView
的應用程序。現在我想添加一個UISearchBar
以允許搜索。搜索真的工作得很好,並選擇正確的單元格。搜索時,您可以看到有可以選擇的單元格,但標籤是空白的。這裏是我的代碼:UITableView中的單元格在UISearchBar中搜索時保持清晰
- (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;
}
這幾乎是完全一樣的問題,因爲這些問題,但這個問題不能得到解決:UISearchBar with UITableView containing custom cells => blank cells和It doesn't show the contents of the cells in a UITableView filtered by a UISearchDisplayController
我真的很感激,如果有人能幫助我!
編輯:
這裏是我的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;
}
你返回正確的行號進行搜索時,也證實了在數組中的值,當你搜索? – Harsh
是的,這部分工作完美。正如你可以在我的代碼中看到的,我通過NSLog檢查了它,然後是否已經寫入標籤,但是沒有。 – timo
你可以請同時分享用searchDisplayController代表 – Harsh