【问题标题】:UITableView contentSize wrong when using UISearchBar使用 UISearchBar 时 UITableView contentSize 错误
【发布时间】:2014-02-11 04:48:10
【问题描述】:

UITableView 报告的contentSize 比使用UISearchBar 时的预期更大。如果单元格为零,则预期的内容高度将为零。相反,以下代码在运行 iOS 7 的 4 英寸 iPhone 中输出 612。

@implementation HPViewController {
    UITableView *_tableView;
    UISearchBar *_searchBar;
    UISearchDisplayController *_searchController;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tableView];

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

    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchController.delegate = self;
    _searchController.searchResultsDataSource = self;
    _tableView.tableHeaderView = _searchBar;
}

- (void)viewDidLayoutSubviews
{
    CGSize contentSize = _tableView.contentSize;
    NSLog(@"%f", contentSize.height); // 612.000000
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; };

@end

注释设置标题视图的行会使代码按预期输出 0。

另外,如果我指定一个空的UIView 作为标题,contentSize 将是正确的并且匹配标题的高度。这个问题只发生在UISearchBar

有没有办法解决这个问题?我是不是做错了什么?

【问题讨论】:

  • 我今天在 iOS 11.2 上遇到了同样的问题。表格中有 1 个标题和 1 个单元格,我几乎可以将所有内容滚动到屏幕外。但是,contentSize 具有我在 iOS 10.3.1 上所期望的高度。

标签: ios ios7 uisearchbar uitableview


【解决方案1】:

从 iOS11 开始,UITableView 默认使用估计行/页眉/页脚高度来计算初始 contentSize。事实上,如果您点击搜索栏并关闭键盘,内容大小将具有正确的值。

要解决此问题,请在 IB 中将估计行、页眉和页脚高度设置为 0,而不是自动:

或者只是以编程方式进行:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

【讨论】:

    【解决方案2】:

    UISearchBar 放在容器UIView 中主要解决了这个问题。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_tableView];
    
        UIView *searchBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        _searchBar = [[UISearchBar alloc] initWithFrame:searchBarContainer.bounds];
        [searchBarContainer addSubview:_searchBar];
    
        _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
        _searchController.delegate = self;
        _searchController.searchResultsDataSource = self;
        _tableView.tableHeaderView = searchBarContainer;
    }
    

    不幸的是,UISearchBar 在某些我还无法隔离的情况下出现故障。我选择通过添加所有单元格的高度来手动计算contentSize

    【讨论】:

    • 感谢添加到子视图的提示。至少对我来说是独立工作的。
    • 有搜索栏自动隐藏在导航栏下的功能。不幸的是,它消失了这个视图容器的 bcs...
    【解决方案3】:

    添加这个委托方法

    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
    

    也许您的页脚视图正在增长,因为没有要显示的单元格。

    【讨论】:

    • 不会被调用。我的示例代码中没有任何部分。 header 是 table view 的 header,而不是 section header。
    猜你喜欢
    • 2012-04-15
    • 2017-07-16
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多