美文网首页iOS开发交流学习
TableView底部出现一块空白

TableView底部出现一块空白

作者: nadou23 | 来源:发表于2018-05-15 16:14 被阅读41次

    TableView的contentSize是会走tableview 的代理方法动态计算,不需要我们手动去设置。当我们要为TableView添加header时请记得先设置两个代理后,再添加header ,否则你会发现TableView底部会多出一块空白。
    上错误代码,这样会底部会多出一块空白

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,   self.view.width, self.view.height) style:UITableViewStyleGrouped]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     _tableView.tableHeaderView = self.headerView;
     _tableView.tableHeaderView.height = self.headerView.height;
      _tableView.showsVerticalScrollIndicator = NO;
      _tableView.showsHorizontalScrollIndicator = NO;
      _tableView.dataSource = self;
      _tableView.delegate = self;
    

    正确代码

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,   self.view.width, self.view.height) style:UITableViewStyleGrouped]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      _tableView.dataSource = self;
      _tableView.delegate = self;
    //添加tableHeaderView在_tableView.dataSource = self后
     _tableView.tableHeaderView = self.headerView;
     _tableView.tableHeaderView.height = self.headerView.height;
      _tableView.showsVerticalScrollIndicator = NO;
      _tableView.showsHorizontalScrollIndicator = NO;
    

    原因是当你先设置TableView添加header时,我们会发现TableView会把contentSize设置得比本来的高度高一点,比如真实内容是H1的高度,但现在是H2 >H1.再设置代理的话,通过走TableView的代理方法设置的contentSize会在H2的基础上进行计算的,这样到最后刷新完,contentSize就会比真实的内容大一点了。

    相关文章

      网友评论

        本文标题:TableView底部出现一块空白

        本文链接:https://www.haomeiwen.com/subject/jdyddftx.html