gpt4 book ai didi

ios - 使用自动布局逐渐显示表格 View 的单元格?

转载 作者:行者123 更新时间:2023-11-30 14:13:36 26 4
gpt4 key购买 nike

我的 UIViewController 中有一个表格 View 来显示评论。该表格 View 的高度取决于评论的数量和大小。细胞是动态的。

我使用自动布局,所以我的表格 View 有高度限制。我以编程方式设置此约束:

override func viewDidLayoutSubviews() {

self.heightCommentsTableView.constant = self.commentsTableView.contentSize.height + 50

}

如果我一次显示所有评论,它就会起作用。

但是,我想使用此方法显示每 5 个评论 5 条: load more for UITableView in swift由于显示我的 View 的性能问题(我的所有评论都是在推送 View 之前加载的)

我注意到,当我设置约束时,它会为所有评论调用 cellForRowAtIndexPath,而不仅仅是前 5 条评论。

我不知道该怎么办。

编辑

var allCommentsArray: NSMutableArray = []
var elements: NSMutableArray = []
var range = 5
var currentPage = 0
var nextpage = 0

override func viewDidLoad() {

super.viewDidLoad()
allCommentsArray = NSMutableArray(array: comments)
elements.addObjectsFromArray(allCommentsArray.subarrayWithRange(NSMakeRange(0, range)))
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}


override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

self.nextpage = self.elements.count - 5

if indexPath.row == nextpage {
self.currentPage++
self.nextpage = self.elements.count - 5
self.elements.addObjectsFromArray(self.allCommentsArray.subarrayWithRange(NSMakeRange(self.currentPage, self.range)))

tableView.reloadData()
}
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("CommentCustomTableViewCell", forIndexPath: indexPath) as! CommentCustomTableViewCell
self.configureCell(cell, atIndexPath: indexPath)

return cell
}

最佳答案

表格 View 将为即将创建的每个可见单元格调用tableView(_:cellForRowAtIndexPath:),其数量将根据有多少个部分以及每个部分中有多少个单元格来确定。您可以通过实现 UITableViewDataSourcetableView(_:numberOfRowsInSection:)numberOfSectionsInTableView(_:) 方法让表格 vie 了解这些金额。因此,如果您想控制在给定时间可能创建和可见的单元格数量,则必须根据您想要的逻辑添加和删除来管理数据源中的状态。在您链接的答案中,您可以看到他被称为 elements.addObjectsFromArray 来逐步批量添加更多元素。

关于ios - 使用自动布局逐渐显示表格 View 的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com