【发布时间】:2014-03-14 04:22:32
【问题描述】:
我有一个包含两个部分的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;
}
【问题讨论】:
-
搜索时是否返回了正确的行数,并在搜索时确认数组中的值是什么?
-
是的,这部分工作得很好。正如您在我的代码中看到的那样,我通过
NSLog检查了它,然后它是否已写入标签,但它没有。 -
能否也分享一下searchDisplayController Delegate中写的代码
-
最后一件事是你使用storyboards还是xib?
-
我正在使用故事板。我的 tableViewController 是委托,所以我会将方法添加到我的帖子中。
标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller