iOS开发tableView设置section高度不正确问题解决

存在问题

当设置分组的tableView时,使用代理设置section高度

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    return [[UIView alloc] init];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

        return 8;

}

得到结果如下,发现设置的高度并不对,代理1返回的view在底部高度为8是正确的,但是整个的高度偏大

检查后发现是sectionfooterView的高度没有设置,两块连在一起导致高度不正确,所以添加代码,将footerView的高度设为0

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    return [[UIView alloc] init];

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0;

}

结果如下

需要注意的是iOS11之后设置sectionHeader和footer的高度需要实现以下两个方法,不然设置高度是无效的

- (UIView)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

猜你喜欢

转载自blog.csdn.net/ljw2017/article/details/85054612