如何从底部向上填充UITableView? [英] How to populate UITableView from the bottom upwards?

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

问题描述

是否可以颠倒tableView的顺序。我已经搜索了很多解决方案,但所有结果都不是我想要实现的解决方案。他们都建议使用 scrollToRowAtIndexPath 滚动到表的最后位置,然后反向填充数据。但是,如果表内容是动态的,并且在某些情况下并非所有单元都有数据,则这不起作用。例如,在正常的tableView中,订单是:

is it possible to reverse the order of a tableView. I have searched a lot for a solution but all the results have not quite been a solution to what I am trying to achieve. They all suggest scrolling to the last position of a table with scrollToRowAtIndexPath and populating the data in reverse. But this doesn't work if the table content is dynamic and in some instances not all the cells have data. For example in a normal tableView the order is:

滚动方向

v

V

scroll direction
v
V

所需的结果是:

滚动方向

^

^

the desired result would be:
scroll direction
^
^

在此示例中,如果我使用 scrollToRowAtIndexPath的建议方法并使用对象数组的长度,我只能从顶部获得第三个单元格。并最终得到这样的结果:

in this example if I used the suggested method of scrollToRowAtIndexPath and use the length of the array of objects, I would only get the third cell from the top. And end up with something like this:

不必要的结果:

滚动方向

v

V

scroll direction
v
V

任何帮助都会非常棒,谢谢。

any help would be great thank you.

推荐答案

要从底部填充表 ,请使用此选项:

To populate table from the bottom use this:

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

要反转元素的顺序,请使用:

To reverse order of elements use this:

dataSourceArray = dataSourceArray.reverseObjectEnumerator.allObjects;

Swift 2.2 版本(已更新):

func updateTableContentInset() {
    let numRows = tableView(self.tableView, numberOfRowsInSection: 0)
    var contentInsetTop = self.tableView.bounds.size.height
    for i in 0..<numRows {
        let rowRect = self.tableView.rectForRowAtIndexPath(NSIndexPath(forItem: i, inSection: 0))
        contentInsetTop -= rowRect.size.height
        if contentInsetTop <= 0 {
            contentInsetTop = 0
        }
    }
    self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0)
}

Swift 3.0+ /4.0 版本(已更新):

func updateTableContentInset() {
    let numRows = tableView(self.tableView, numberOfRowsInSection: 0)
    var contentInsetTop = self.tableView.bounds.size.height
    for i in 0..<numRows {
        let rowRect = self.tableView.rectForRow(at: IndexPath(item: i, section: 0))
        contentInsetTop -= rowRect.size.height
        if contentInsetTop <= 0 {
            contentInsetTop = 0
        }
    }
    self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0)
}

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

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