UITableViewStyleGrouped模式下烦人的多余间距

第一个section上边多余间距处理

// 隐藏UITableViewStyleGrouped上边多余的间隔
_tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

每个section下边多余间距处理

// 隐藏UITableViewStyleGrouped下边多余的间隔
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
  • 备注:若传入的 height == 0,则 height 被设置成默认值
  • 若 height 小于屏幕半像素对应的高度,则不会被渲染,所以这里返回CGFLOAT_MIN,其实返回0.01也是可以的

补充:代码顺序的不同导致第一个section上边出现多余间距

  • 在设置代理设置tableFooterView,上边会出现多余间距
tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.tableFooterView = [UIView new];
tableView.delegate = self;
tableView.dataSource = self;
  • 在设置代理设置tableFooterView,上边不会出现多余间距
tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableFooterView = [UIView new];
  • 可以通过第一个section上边多余间距处理的办法来解决因代码顺序导致的上述问题,所以这里建议要解决第一个section上边多余间距还是通过文章开头所说的解决办法更好

参考文章

  • 0代码隐藏GroupedTableView上边多余的间隔
  • UITableViewStyleGrouped类型的UITabelView使用技巧

你可能感兴趣的:(UITableViewStyleGrouped模式下烦人的多余间距)