tableview优化

使用UITableViewDelegate代理方法来设置head

2022-07-17  本文已影响0人  __Mr_Xie__

问题描述

使用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的高度和父控件的高度一致。

剖析

举例:当我们使用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...

上一篇 下一篇

猜你喜欢

热点阅读