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

查看:33
本文介绍了如何从底部向上填充 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.

推荐答案

从底部填充UITableView :

- (void)updateTableContentInset {
    NSInteger numRows = [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);
}

要颠倒元素的顺序:

dataSourceArray = dataSourceArray.reverseObjectEnumerator.allObjects;

Swift 4.2/5 版本:

func updateTableContentInset() {
    let numRows = self.tableView.numberOfRows(inSection: 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
            break
        }
    }
    self.tableView.contentInset = UIEdgeInsets(top: contentInsetTop,left: 0,bottom: 0,right: 0)
}

Swift 3/4.0 版本:

self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0)

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

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