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;