ios – 将UISearchBar与表视图控制器一起使用并转换到另一个视图时显示问题

从iOS 8开始,我遇到了一个关于表视图/ UISearchBar设置的奇怪问题,并且想知道其他人是否遇到过类似的问题,或者可以指出什么,如果有的话,我可能做错了.广泛的情况:

>我有一个UITableViewController,其中包含一个UISearchBar,在应用程序的Storyboard中设置
>表格视图还有一个自定义单元格,再次在故事板中设置
>选择表格行会触发到另一个视图的segue
>执行搜索,从搜索结果中点击一行以切换到另一个视图,然后再次导航,触发各种问题.

“问题”是如果我按如下方式实现cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = (MyCell *) [self.tableView dequeueReusableCellWithIdentifier:@"MyId" forIndexPath:indexPath];
...

换句话说,通过指定dequeueReusableCellWithIdentifier的路径,这会导致iOS 8中的BAD_ACCESS或断言失败(但不会导致iOS 7).具体来说,在上述情况下,在调用dequeueReusableCellWithIdentifier时发生断言失败或BAD_ACCESS,即,当搜索处于活动状态时,您从结果表中的一个单元格转到另一个视图,然后再次向后转换.

现在,我可以通过调用来阻止错误发生:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = (MyCell *) [self.tableView dequeueReusableCellWithIdentifier:@"MyId"];
...

没有传入indexPath.这样就可以正常工作,但是在使用搜索结果回到表格视图时,会出现奇怪的显示问题,从而在搜索结果下面分层,似乎是“鬼”表的分隔符,几乎就像系统正在尝试将一个表直接呈现在另一个表之上(但不为每个表调用cellForRowAtIndexPath,仅针对搜索结果表按预期调用).

无论segue是附加到单元格还是表视图控制器,我都会遇到同样的问题(所以在后一种情况下,我实现了didSelectRowAtIndexPath来手动触发segue).

那么:(a)任何人都可以指出我可能做错的事情导致这些问题,或者(b)指向一个带有UISearchBar的表视图控制器的简单工作示例,其中表格单元格转换到另一个视图?我很惊讶我遇到了很多问题,因为实现一个带有详细视图的可搜索表必须是一个人们常常做的常见,无聊的事情,不是吗?

展示iusse的示例项目:http://www.javamex.com/DL/TableTest.zip

解决方法

使用这两种方法为主tableView出列单元格实际上没有任何问题,尽管indexPath变体似乎是Apple目前的首选选项.

但是对于searchResultsTableView,请避免指定indexPath,因为它不一定对视图控制器的tableView有效.那是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell;
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    } else {
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    }
    // configure the cell
}

为了使其正常工作,您还需要修改其他UITableViewDataSource方法.代替:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (searchResults) ? (searchResults.count) : (testData.count);
}

做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
        return searchResults.count;
    }
    return testData.count;
}

以上是来客网为你收集整理的ios – 将UISearchBar与表视图控制器一起使用并转换到另一个视图时显示问题全部内容,希望文章能够帮你解决ios – 将UISearchBar与表视图控制器一起使用并转换到另一个视图时显示问题所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。