ios - 计算适合固定空间的 UITableViewCells 的数量

标签 ios swift

我有一个固定大小的空间,我将在其中放置一个 UITableView。我不希望 UITableView 滚动,所以我想计算无需滚动即可容纳的单元格数。单元格的大小各不相同,并使用 UITableViewAutomaticDimension 来确定大小。解决此问题的最佳方法是什么?

最佳答案

您可以使用此方法并将单元格高度保存在字典中,然后您可以对字典值运行 cicle 并对所有保存的值求和,然后使用这些值执行您需要的操作,让我们看一些示例代码,

首先定义字典来保存你的值

var automaticDimensionsHeightRows : [Int:CGFloat] = [:]

我定义了这个 var automaticDimensionsHeightRows 变量,因为我有两种类型的单元格,一种是固定高度的,另一种是 UITableViewAutomaticDimensionheight

然后用这个方法我们得到,并保留值

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/

相关文章:

IOS 潜在泄漏对象消息

ios - iOS 中的 CocoaPods 是什么?

objective-c - 为 REST API 开发 iOS SDK

swift - 通过 Swift 中的迭代器向后遍历字符串

swift - 位置更改时 Realm 更新/添加

swift - 进行异步调用后运行 Modal

ios - 搜索栏不会出现在 ios 中

ios - Facebook Messenger SDK 分享文本和 url 给 facebook 好友

ios - 为什么调用 reloadRows 后在数组中找不到 heightForRowAt 值?

ios - swift post请求编码加号变为空白