如何在屏幕底部定位UITableViewCell?

ohh*_*hho 26 cocoa-touch uitableview ios

一个到三个UITableViewCells UITableViewView.之后是否有办法始终将单元格放置在屏幕底部reloadData

+----------------+     +----------------+     +----------------+
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     |                |
|                |     |                |     | +------------+ |
|                |     |                |     | |   cell 1   | |
|                |     |                |     | +------------+ |
|                |     | +------------+ |     | +------------+ |
|                |     | |   cell 1   | |     | |   cell 2   | |
|                |     | +------------+ |     | +------------+ |
| +------------+ |     | +------------+ |     | +------------+ |
| |   cell 1   | |     | |   cell 2   | |     | |   cell 3   | |
| +------------+ |     | +------------+ |     | +------------+ |
+----------------+     +----------------+     +----------------+
Run Code Online (Sandbox Code Playgroud)

Bre*_*nan 13

我已经创建了一个新的示例解决方案,因为之前的答案并没有过时的现代用途.

最新技术使用autolayout和self-sizing单元格,因此之前的答案将不再有效.我重新设计了解决方案以使用现代功能,并创建了一个示例项目来放置GitHub.

这个代码不是计算每行的高度,而是导致额外的工作,而是获取最后一行的帧,以便可以计算从顶部开始的内容插入.它利用了表视图已经在做的事情,因此不需要额外的工作.

此代码也仅设置顶部插入,以防为键盘或其他叠加设置底部插入.

请报告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) {
    }];
}
Run Code Online (Sandbox Code Playgroud)


lie*_*sfl 10

每当添加一行时调用此方法:

- (void)updateContentInset {
    NSInteger numRows=[self tableView:_tableView numberOfRowsInSection:0];
    CGFloat contentInsetTop=_tableView.bounds.size.height;
    for (int i=0;i<numRows;i++) {
        contentInsetTop-=[self tableView:_tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        if (contentInsetTop<=0) {
            contentInsetTop=0;
            break;
        }
    }
    _tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

  • 更简洁的方法:`self.tableView.contentInset = UIEdgeInsetsMake(MAX(0.0,self.tableView.bounds.size.height - self.tableView.contentSize.height),0,0,0);`.表视图的内容大小是所有单元格的大小. (3认同)

Pat*_*her 5

您可以在表格视图中设置标题,并使其足够高以推动第一个单元格.然后相应地设置tableView的contentOffset.我不认为有一个快速的内置方式来做到这一点.