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