ios - 搜索显示 Controller 结果 TableView 无法在 iOS 6 中滚动

标签 ios objective-c uitableview uisearchbar uisearchdisplaycontroller

我在 UISearchDisplayController 的表格 View 中遇到了这个奇怪的错误,它只发生在 iOS 6 中。我刚刚在 nib 文件中创建了我的表格 View ,然后以编程方式在其上方添加了一个搜索栏,然后用于过滤 TableView 中数据的搜索显示 Controller :

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.mpSearchBar = [[MPSearchBar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
    self.mpSearchBar.placeholder =@"Card Search";

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.mpSearchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;  

    self.resultTableView.delegate = self;
    self.resultTableView.dataSource = self;
    [self.resultTableView reloadData];

}

当我第一次在搜索栏中输入一些查询时,搜索显示 Controller 过滤数据并且 Controller 的结果 TableView 工作正常。但是,当我点击搜索栏中的清除按钮并输入其他内容时,包含新过滤数据集的结果 TableView 无法长时间滚动。奇怪的是,当我尝试记录表格 View 的 framecontentSize 时,内容大小高度大于框架高度,因为它应该是:

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {

    tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
    tableView.contentInset = UIEdgeInsetsZero;
    [tableView hideEmptySeparators];

    if (IOS_EQUAL_OR_NEWER_THAN_7){
        tableView.separatorInset = UIEdgeInsetsZero;
    }

    NSLog(@"Frame height %f, Content height %f", tableView.frameHeight, tableView.contentSize.height);
}

这是我从日志中得到的:

Frame height 504.000000, Content height 1402.000000

只有当我在 iOS 6 设备上测试并且我不知道如何调试这个问题时才会发生这种情况。

请建议并感谢。

最佳答案

事实证明,这是 iOS 6 中 UISearchController 的 TableView 的一个(不是众所周知的)问题。我的临时解决方案是从 获取 contentSize willShowSearchResultsTableView 并以编程方式将其设置为 viewDidLayoutSubviews 中的 TableView :

- (void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if ([self.searchController isActive]){
        // fix wrong content size due to search bar glitch in iOS 6
        self.searchController.searchResultsTableView.contentSize = newContentSize;
    }
}

希望这能帮助遇到与我相同问题的任何人。

关于ios - 搜索显示 Controller 结果 TableView 无法在 iOS 6 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708951/

相关文章:

iOS确定CBCharacteristic的数据长度

iOS 为什么不能方法 collectionView :didSelectItemAtIndexPath: be called from UITapGestureRecognizer?

ios - 如何使用对象数组填充 TableView?

ios - cellForRowAtIndexPath 在设备旋转期间不调用

ios - 将文件路径转换为文件 url[NSUrl]

ios - 如何确定在 iOS 上安装后有多少用户仍在使用该应用程序?

objective-c - 在 NSArray 中选择一个随机对象

ios - UITableView 自定义单元格不工作

ios - 使用 PanGestureRecognizer 在与另一个 View 碰撞之前检测 View 的最后一个中心点?

iphone - AVAudioPlayer 关闭 iPod - 如何解决?