2016-03-15 115 views
1

我以编程方式创建了UITableView,但它在下面有一些额外空间(精确为29像素)。iOS:UITableView底部的额外空间

这是我的初始化表:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; 
[tableView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.contentView addSubview:tableView]; 
self.alertsTableView = tableView; 

这里有约束:

@"V:|-10-[_alertsTableView(0)]-5-[_optionsLabel]", 
    @"H:|-15-[_alertsTableView]-15-|", 

我试过页脚视图设置为零,并为它的高度返回零:

[self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]; 

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
    return CGFLOAT_MIN; 
} 

我动态地改变它的高度,根据内容:

- (void)updateHeight:(CGFloat)height { 
    if (self.tableView.superview) { 
     NSLayoutConstraint *heightConstraint; 
     for (NSLayoutConstraint *constraint in self.tableView.superview.constraints) { 
      if (constraint.firstAttribute == NSLayoutAttributeHeight) { 
       heightConstraint = constraint; 
       break; 
      } 
     } 
     heightConstraint.constant = height; 
    } 
} 

我也将setAutomaticallyAdjustsScrollViewInsets设置为NO。并且this是最终结果。单元格和空白空间之间有一个分隔符,但底部不是单元格。

+1

您还可以设置footerview使用。 table.tableFooterView = [uiview new]; –

+0

尝试“Debug View Hierarchy”按钮 - 可以帮助您查看哪个UIView占用了空间。 – siburb

+0

你可以分享代码如何计算“高度”。此外,我会建议改变方法如何更新约束。而不是运行所有的约束条件,你可以在第一时间拥有一个用于高度约束的ivar,并且可以在'updateHeight:'方法中直接更新它的值。 – Gandalf

回答

8

您可以使用contentInset补偿29px额外的下边距:

tableView.contentInset = UIEdgeInsetsMake(0, 0, -29, 0);