gpt4 book ai didi

ios - 如何将 UITableViewCell 定位在屏幕底部?

转载 作者:IT王子 更新时间:2023-10-29 07:54:03 29 4
gpt4 key购买 nike

一个UITableViewView中有一到三个UITableViewCell。有没有办法在 reloadData 之后始终将单元格定位在屏幕底部?

+----------------+     +----------------+     +----------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | +------------+ |
| | | | | | cell 1 | |
| | | | | +------------+ |
| | | +------------+ | | +------------+ |
| | | | cell 1 | | | | cell 2 | |
| | | +------------+ | | +------------+ |
| +------------+ | | +------------+ | | +------------+ |
| | cell 1 | | | | cell 2 | | | | cell 3 | |
| +------------+ | | +------------+ | | +------------+ |
+----------------+ +----------------+ +----------------+

最佳答案

我已经创建了一个新的示例解决方案,因为以前的答案对于现代使用来说还不算过时。

最新技术使用自动布局和自动调整单元格大小,因此之前的答案将不再有效。我重新设计了解决方案以使用现代功能并创建了一个示例项目以放在 GitHub 上。

这段代码没有计算每行的高度,这会导致额外的工作,而是获取最后一行的框架,以便可以计算从顶部插入的内容。它利用了 TableView 已经在做的事情,因此不需要额外的工作。

此代码也仅设置顶部插图,以防为键盘或其他覆盖设置底部插图。

请在 GitHub 上报告任何错误或提交改进,我将更新此示例。

GitHub:https://github.com/brennanMKE/BottomTable

- (void)updateContentInsetForTableView:(UITableView *)tableView animated:(BOOL)animated {
NSUInteger lastRow = [self tableView:tableView numberOfRowsInSection:0];
NSUInteger lastIndex = lastRow > 0 ? lastRow - 1 : 0;

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:lastIndex inSection:0];
CGRect lastCellFrame = [self.tableView rectForRowAtIndexPath:lastIndexPath];

// top inset = table view height - top position of last cell - last cell height
CGFloat topInset = MAX(CGRectGetHeight(self.tableView.frame) - lastCellFrame.origin.y - CGRectGetHeight(lastCellFrame), 0);

UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = topInset;

UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:animated ? 0.25 : 0.0 delay:0.0 options:options animations:^{
tableView.contentInset = contentInset;
} completion:^(BOOL finished) {
}];
}

关于ios - 如何将 UITableViewCell 定位在屏幕底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285687/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com