我有桌面视图,当我触摸单元格时,会出现detailTextLabel。我实现了搜索,但是didSelectRowAtIndexPath对搜索结果数组不起作用。我刚从cellForRowAtIndexPath复制了代码。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
self.searchResults = [self.cards filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Card Cell" forIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
Card *card = [self.searchResults objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"otherSide"]]];
cell.detailTextLabel.hidden = YES;
} else {
Card *card = [self.cards objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"otherSide"]]];
cell.detailTextLabel.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView beginUpdates];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Card Cell" forIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
Card *card = [self.searchResults objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"otherSide"]]];
cell.detailTextLabel.hidden = NO;
} else {
Card *card = [self.cards objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [card valueForKey:@"otherSide"]]];
cell.detailTextLabel.hidden = NO;
}
[self.tableView endUpdates];
}
答案 0 :(得分:0)
cellForRowAtIndexPath
和didSelectRowAtIndexPath
之间存在一些显着差异。 cellForRowAtIndexPath
创建一个单元格或获取现有的单元格堆栈,您将输入属性并在显示之前将其返回。在didSelectRowAtIndexPath中,您不会在参数中获取该单元格,因此您必须调用[tableview cellForRowAtIndexPath]
而不是[self.tableView dequeueReusableCellWithIdentifier:@"Card Cell" forIndexPath:indexPath];
来获取单元格并设置属性。
还要确保已设置tableview的委托。