我已经创建了一个非常简单的测试用例来重现这个问题。
我正在尝试以编程方式将页脚视图设置为表视图。请注意,我指的是表视图最底部的页脚,而不是区段页脚(大多数堆栈溢出答案都会混淆它们)。
下面是我非常简单的代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *footerContainer = [[UIView alloc] initWithFrame:CGRectZero];
footerContainer.backgroundColor=[UIColor greenColor];
footerContainer.translatesAutoresizingMaskIntoConstraints=NO;
[footerContainer addConstraints:@[[NSLayoutConstraint
constraintWithItem:footerContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100
],
[NSLayoutConstraint
constraintWithItem:footerContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:[UIScreen mainScreen].bounds.size.width
]]];
self.mytableview.tableFooterView=footerContainer;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}然而,结果看起来像这样:

正如您注意到的,页脚显示在表视图的顶部。这是一个bug,还是我漏掉了什么?
如果我将tableFooterView更改为tableHeaderView,那么它可以正常工作。所以我希望footer也能用同样的方法,但事实并非如此。
发布于 2018-05-13 23:19:00
请勿在分配给的UIView上设置tableView.tableFooterView tableView.tableFooterView。
在您的示例中,这是footerContainer。我也有同样的问题。问题的评论中提到了这一点,但我仍然花了几个小时进行故障排除,然后才注意到我正在做这件事,所以我把它放在这里作为一个可能的答案。
https://stackoverflow.com/questions/46411402
复制相似问题