使用tableHeaderView iOS 7中的UISearchBar进行错误访问

时间:2013-09-19 19:26:57

标签: iphone uitableview uisearchbar exc-bad-access ios7

我正在尝试在UITableview的tableHeaderView中使用UISearch栏。这个搜索功能在iOS 6上没有问题,但是自从7.0 SDK构建以来,在从另一个视图返回后点击搜索栏时出现崩溃。第一次查看搜索tableView时,搜索栏的工作方式与预期一致。但是,当我深入查看详细视图时,搜索栏会在我点击它时抛出一个错误访问异常。在我的方案中使用NSZombies不会记录此错误访问。以下是设置标题视图的代码:

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    self.searchController = [[UISearchDisplayController alloc]
                             initWithSearchBar:self.searchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;

    self.tableView.tableHeaderView = self.searchBar;

属性self.searchBarself.searchController都被声明为强引用。在任何地方都没有收到任何内存警告。 iOS 7中的UISearchBar或tableHeaderView有什么变化吗?值得注意的是,我正在将外观代理应用于UISearchBar,但它在首次加载时看起来和正常工作。

2 个答案:

答案 0 :(得分:0)

我遇到了iOS7中UISearchBar崩溃的问题,直到我开始将UISearchDisplayController设置为非活动状态才解除UITableViewController。添加以下内容对我有用,但我不知道我们的问题是否相关:

[_mySearchDisplayController setActive:NO];

答案 1 :(得分:0)

原因是崩溃是由于使用sectionIndexTitlesForTableViewUISearchDisplayController(请参阅this question)。这似乎是iOS 7 SDK中的一个错误。相反,我现在使用CMIndexBar作为第三方替代品。就像本机部分索引一样工作,永远不会崩溃,可以轻松定制。