如何从底部向上填充UITableView?

编程入门 行业动态 更新时间:2024-10-28 00:24:41
本文介绍了如何从底部向上填充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?

本文发布于:2023-07-16 07:22:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!