如何将UITableViewCell与UITableView的底部对齐? [英] How to align UITableViewCell to the bottom of the UITableView?

查看:129
本文介绍了如何将UITableViewCell与UITableView的底部对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您插入带有 insertRowsAtIndexPaths:withRowAnimation:的第一个 UITableViewCell 时,它通常显示在 UITableView 。在 Periscope应用中,情况恰好相反-第一个插入的单元格底部对齐。随着新单元格的推入,旧单元格在表格中向上移动。如何实现的?

When you insert your first UITableViewCell with insertRowsAtIndexPaths:withRowAnimation:, it usually appears at the top of the UITableView. In the Periscope app, the opposite happens - the first inserted cell is bottom aligned. As new cells are pushed in, the old cells move up in the table. How is this achieved?

推荐答案

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

In case you're interested in how I did it in the Periscope iOS app, it's actually pretty simple...

TL; DR;添加高度等于表格视图框架高度的透明表格标题视图。然后,当您向表中添加单元格时,只需为表视图的内容偏移设置动画。

TL;DR; Add a transparent table header header view with a height equal to your table view frame's height. Then, as you add cells to your table, simply animate the table view's content offset.

为表视图提供标题视图:

Give your table view a header 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)。然后调用此方法为以下单元格设置动画:

Add data to your table (in my case, messages get added to an array called _messages. I then call reloadData on the UITableViewController). Then call this method to animate the cells in:

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

希望有帮助。我发现这是模拟锚定在底部的单元格的一种非常便宜/简单的方法。我知道有人提到过将桌子倒置,但这对我来说似乎很疯狂。 :-)

Hope that helps. I found this to be a pretty cheap/simple way of simulating cells anchored to the bottom. I know some people have mentioned flipping the table upside down, but that just seems crazy to me. :-)

这篇关于如何将UITableViewCell与UITableView的底部对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆