ios - 如何将 UITableViewCell 对齐到 UITableView 的底部?

标签 ios uitableview cocoa-touch

当您使用 insertRowsAtIndexPaths:withRowAnimation: 插入第一个 UITableViewCell 时,它通常出现在 UITableView 的顶部。在Periscope app ,相反的情况发生 - 第一个插入的单元格底部对齐。随着新单元格被插入,旧单元格在表格中向上移动。这是如何实现的?

enter image description here

最佳答案

如果您对我在 Periscope iOS 应用程序中的实现方式感兴趣,它实际上非常简单...

长话短说;添加一个高度等于表格 View 框架高度的透明表格标题标题 View 。然后,当您向表格中添加单元格时,只需为表格 View 的内容偏移设置动画即可。

给你的 TableView 一个标题 View :

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    headerView.userInteractionEnabled = NO; // all touches within this space must go through to the video layer

    return headerView;   // empty header above chat, so messages flow in from bottom
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.tableView.frame.size.height;            // empty header above chat, so messages flow in from bottom
}

将数据添加到您的表(在我的例子中,消息被添加到一个名为 _messages 的数组中。然后我在 UITableViewController 上调用 reloadData)。然后调用此方法为单元格设置动画:

- (void)scrollTableToBottom
{
    if (!self.isViewLoaded || _messages.count == 0)
        return;

    CGFloat offsetY = self.tableView.contentSize.height - self.tableView.frame.size.height + self.tableView.contentInset.bottom;

    [UIView animateWithDuration:0.33
            delay:0
            options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
            animations:^{
                [self.tableView setContentOffset:CGPointMake(0, offsetY) animated:NO];
            }
            completion:nil];
}

希望对您有所帮助。我发现这是一种非常便宜/简单的模拟固定在底部的细胞的方法。我知道有些人提到过把 table 翻过来,但我觉得这很疯狂。 :-)

关于ios - 如何将 UITableViewCell 对齐到 UITableView 的底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997011/

相关文章:

ios - 使用 - observeValueForKeyPath :ofObject:change:context: in Swift 3

ios - 我想删除 tableview 上的一行,但收到​​错误消息?

Objective-c/iOS - 使用 Safari 打开本地 html 文件

iphone - 如何设置UILabel非矩形框

cocoa-touch - iOS indexPath 为零(自定义附件图像)

android - Android 如何像在 iOS 中一样广播 BLE 本地名称?

iphone - 在 iPhone 和 iPad 上显示网站的整个宽度

ios - Swift Firebase 在创建新用户时说内部错误

objective-c - 可重复使用的 UITableViewCells 重复内容

ios - 仅为 UITableView 中可用的 CellForRow 显示分隔符