Ios 如何将UITableViewCell与UITableView的底部对齐?

Ios 如何将UITableViewCell与UITableView的底部对齐?,ios,uitableview,cocoa-touch,Ios,Uitableview,Cocoa Touch,使用insertrowstindexpath:withRowAnimation:插入第一个UITableView单元格时,它通常显示在UITableView的顶部。在中,情况正好相反-第一个插入的单元格是底部对齐的。当新单元格被推入时,旧单元格在表格中向上移动。这是如何实现的 您可以使用insertRowsAtIndexPath方法,然后让tableview滚动到底部 [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathF

使用
insertrowstindexpath:withRowAnimation:
插入第一个
UITableView单元格时,它通常显示在
UITableView
的顶部。在中,情况正好相反-第一个插入的单元格是底部对齐的。当新单元格被推入时,旧单元格在表格中向上移动。这是如何实现的


您可以使用insertRowsAtIndexPath方法,然后让tableview滚动到底部

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.posts.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
如果您希望在tableview的顶部有一个巨大的间隙,您可以设置滚动偏移,并在新项目出现时进行调整


另一种方法是将tableview倒置,然后将每一行倒置

如果你对我在Periscope iOS应用程序中的操作感兴趣,其实很简单

TL;博士添加高度等于表视图边框高度的透明表标题视图。然后,在向表格添加单元格时,只需设置表格视图内容偏移的动画即可

为表视图提供标题视图:

- (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];
}

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

Aaron Wasserman答案的Swift 4版本

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: .zero)
    headerView.isUserInteractionEnabled = false
    return headerView
}

// Added logic to avoid blank space at the top
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return tableView.frame.size.height - CGFloat(messages.count * cellHeight)
}

 func scrollToBottom(animated: Bool) {
    if isViewLoaded && messages.count > 0 {

        let offset = tableView.contentSize.height - tableView.frame.size.height + tableView.contentInset.bottom

        UIView.animate(withDuration: 0.33, delay: 0, options: [.curveEaseOut, .allowUserInteraction], animations: {
            self.tableView.setContentOffset(CGPoint(x: 0, y: offset), animated: animated)
        }, completion: nil)
    }
}

scrollToBottom方法需要在tableView.reloadData()之后调用。

我对Wasserman先生答案的修改

extension MyViewController : UITableViewDelegate {

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return UITableView.automaticDimension
  }

  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
      let headerView = UIView(frame: .zero)
      headerView.isUserInteractionEnabled = false
      return headerView
  }

  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
      let diff = tableView.contentSize.height - tableView.bounds.height
      return diff > 0 ? 0 : -diff
  }

}

1) 您确定这是带有
UITableViewCell
s的
UITableView
吗?2) 假设是,您确定第一个屏幕截图中的
UITableView
顶部与第二个屏幕截图中的
UITableView
顶部位于同一位置吗?@nhgrif我不确定他们是如何实现的。我知道完全不使用表可以实现这种效果,但使用标准表框架将是理想的,因为它可以为您处理单元内存管理和动画。至于相框。原点。y在两张照片中是相同的,在注入新单元格时,很难协调将整个表格向上滑动,因为动画持续时间和单元格注入曲线无法公开访问。我没说这很容易。我想知道是否可以从足够多的空单元格开始填充屏幕,以及在添加新单元格时保持表格视图滚动到底部。添加单元格时,您会看到什么动画?@rdlmar这是个好主意。在上面放一个大的空白单元格。动画效果可以在上看到(他们有一个视频演示)。你好@Aaron Wasserman,谢谢你发布代码。你能建议一些淡出动画的方法吗?我尝试了很多解决方案,但都没有达到要求的效果。我的每个单元格都有一个视图模型,可以跟踪它出现在屏幕上的时间和它开始淡出的时间。然后,在重用单元并设置其新视图模型后,计算动画从何处开始。诀窍是使用可以轻松应用或从层中移除的动画。感谢分享@AaronWasserman,我用180度变换tableview和tableview单元格。tableView.transform=CGAffineTransformMakeRotation(-M_PI);cell.transform=CGAffineTransformMakeRotation(M_PI);我试图删除RowatineXpath,但它看起来不像潜望镜那么好,所以你能指导我如何删除具有淡出效果的旧评论吗?@AaronWasserman谢谢你发布这篇文章。使用自动调整单元格大小时,我有一些奇怪的滚动行为。你能解释一下你是如何配置它来处理动态高度单元格的吗?谢谢