我有一个固定大小的空间,我将在其中放置一个 UITableView。我不希望 UITableView 滚动,所以我想计算无需滚动即可容纳的单元格数。单元格的大小各不相同,并使用 UITableViewAutomaticDimension 来确定大小。解决此问题的最佳方法是什么?
最佳答案
您可以使用此方法并将单元格高度保存在字典中,然后您可以对字典值运行 cicle 并对所有保存的值求和,然后使用这些值执行您需要的操作,让我们看一些示例代码,
首先定义字典来保存你的值
var automaticDimensionsHeightRows : [Int:CGFloat] = [:]
我定义了这个 var automaticDimensionsHeightRows
变量,因为我有两种类型的单元格,一种是固定高度的,另一种是 UITableViewAutomaticDimension
height
然后用这个方法我们得到,并保留值
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if((self.tableView.delegate?.tableView!(self.tableView, heightForRowAt: indexPath))! == UITableViewAutomaticDimension)
{
self.automaticDimensionsHeightRows[indexPath.row] = cell.frame.size.height
}
}
最后,您可以对字典值运行 cicle 并检查您的值或做任何您需要做的事情
希望对你有帮助
关于ios - 计算适合固定空间的 UITableViewCells 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44348896/