美文网首页tableview优化
使用UITableViewDelegate代理方法来设置head

使用UITableViewDelegate代理方法来设置head

作者: __Mr_Xie__ | 来源:发表于2022-07-17 00:10 被阅读0次

问题描述

使用UITableViewDelegate代理方法来设置headerfooter时要注意了。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}

UITableViewUI布局中使用的场景很多,比如下图:

使用场景:让UITableViewcontentSize的高度和父控件的高度一致。

  • 没有使用分组(即没有设置headerfooter),这个时候通过获取UITableViewcontentSize的高度来设置父控件的高度,显示出来的效果是正常的;
  • ⚠️⚠️使用了分组(即设置了headerfooter),这个时候通过获取UITableViewcontentSize的高度来设置父控件的高度,显示出来的效果是不正常的;

剖析

举例:当我们使用UITableViewDelegate代理方法来设置footer时,先调用
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
然后调用
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
但是当我们设置footer的高度为0CGFLOAT_MIN时,执行完
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
就不会调用
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
所以这个时候通过获取UITableViewcontentSize的高度来设置父控件的高度,显示出来的效果是不正常了。

解决方案

当我们使用UITableViewDelegate代理方法来设置headerfooter时,尽量不要给headerfooter的高度设为0CGFLOAT_MIN,应该设置一个很小的高度,eg12...

相关文章

网友评论

    本文标题:使用UITableViewDelegate代理方法来设置head

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