【问题标题】:didSelectRowAtIndexPath does not work when uitableview datasource is filtered过滤 uitableview 数据源时 didSelectRowAtIndexPath 不起作用
【发布时间】:2012-04-23 12:44:53
【问题描述】:

我有 uitableview 和一个带有 searchDisplayController 的搜索栏,我用数据库中的数据填充 tableview 数据源,我使用用户搜索栏进行搜索,一次 uitableview 数据源没有被过滤,didSelectRowAtIndexPath 工作正常,它进入 detailviewcontroller,但是当我使用搜索和过滤数据源,然后如果我选择一个单元格,didSelectRowAtIndexPath 方法不会触发。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KardexViewController* kardexViewController = [[KardexViewController alloc] initWithStyle:UITableViewStylePlain];

    [self.navigationController pushViewController:kardexViewController animated:YES];
}

这就是我添加 uisearchbar 的方式

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataSource = [[NSMutableArray alloc] init];

    self.navigationItem.title = Title;

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; 
}

以及我如何过滤数据源

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSMutableArray* result = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.allData count]; i++) {
        NSRange range = [[[self.allData objectAtIndex:i] valueForKey:@"Code"] rangeOfString:searchString];

        if(range.length > 0)
            [result addObject:[self.allData objectAtIndex:i]];

        range = [[[self.allData objectAtIndex:i] valueForKey:@"Name"] rangeOfString:searchString];

        if(range.length > 0)
            [result addObject:[self.allData objectAtIndex:i]];
    }

    self.dataSource  = [result copy];
    [result release];
    [self.searchDisplayController.searchResultsTableView reloadData];

    return YES;
}

-(void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.dataSource  = [self.allData copy];
}

所以谢谢你的提前

【问题讨论】:

  • 请提供代码,说明您如何使用搜索和归档其中的表格会有一些问题,因此它无法正常工作或根本无法工作

标签: iphone uitableview


【解决方案1】:

经过一番搜索,我发现我应该再设置一件事

searchDisplayController.searchResultsDelegate = self;

【讨论】:

  • 感谢您回来回答您自己的问题。我已经摸索了一段时间想解决这个问题 searchDisplayController.delegate = self;够了,还不够,我需要添加你的代码,现在一切都很好。
  • @Ali,UISearchDisplayController 在 iOS 8 中已弃用。现在有什么解决方案?可以和我分享一下吗?
  • @NarasimhaNallamsetty 很抱歉,很久没有在原生移动应用上工作了。
猜你喜欢
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
相关资源
最近更新 更多