如何从底部向上填充 UITableView?

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

滚动方向vV

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:

不想要的结果:

滚动方向vV

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?

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

发布评论

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

>www.elefans.com

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