Objective c didSelectRowAtIndexPath不适用于搜索结果
我有表格视图,当我触摸单元格时,detailTextLabel出现。我实现了搜索,但DidSelectRowatineXpath不适用于搜索结果数组。我刚从cellForRowAtIndexPath复制了代码Objective c didSelectRowAtIndexPath不适用于搜索结果,objective-c,uitableview,Objective C,Uitableview,我有表格视图,当我触摸单元格时,detailTextLabel出现。我实现了搜索,但DidSelectRowatineXpath不适用于搜索结果数组。我刚从cellForRowAtIndexPath复制了代码 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predica
- (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];
}
cellForRowAtIndexPath
和didSelectRowAtIndexPath
之间存在一些显著差异cellForRowAtIndexPath
创建一个单元格或获取单元格堆栈的现有属性,然后输入属性并在其显示之前返回。在didSelectRowAtIndexPath中,您无法在参数中获得该单元格,因此您必须调用[tableview cellForRowAtIndexPath]
,而不是[self.tableview dequeueReusableCellWithIdentifier:@“Card cell”forIndexPath:indexPath]
获取单元格并设置属性
还要确保已设置tableview的委托。什么是“不工作”?您设置了代理吗?这意味着,当我从self.cards数组中选择项目时,会出现detailTextLabel,但当我从searchResults数组中选择项目时,不会出现这种情况。我刚刚在头文件中设置了代理,那么我应该如何在didSelectRowAtIndexPath中调用[tableview cellForRowAtIndexPath]?对不起,我是一名初级开发人员。