【发布时间】:2016-03-15 08:38:40
【问题描述】:
我以编程方式创建了UITableView,但它下方有一些额外的空间(准确地说是 29 像素)。
这是我初始化表格的方式:
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-|",
我尝试将页脚视图设置为 nil 并为其高度返回零:
[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 是最终结果。单元格和空白区域之间有分隔符,但底部不是单元格。
【问题讨论】:
-
你也可以设置footerview使用。 table.tableFooterView = [uiview new];
-
试试“Debug View Hierarchy”按钮 - 可能会帮助您了解哪个 UIView 占用了空间。
-
您能否分享您如何计算
height的代码。另外,我建议您更改更新约束的方法。您可以首先使用 ivar 作为高度约束,而不是遍历所有约束,并且可以直接在updateHeight:方法中更新它的值。 -
@Gandalf 我只是在
cellForRowAtIndexPath:中设置它等于contentSize.height。我会考虑你的建议,问题是,所有这一切都发生在三个不同的类之间(委托方法在一个单独的委托类中,视图控制器和一个视图,表所在的位置,它是视图控制器的子视图) . -
您可能需要设置 contentInset 值。检查这个:developer.apple.com/library/ios/documentation/WindowsViews/…
标签: ios objective-c uitableview