ios - 快速动态的 UITableView 行高

标签 ios swift uitableview

我的表格中间有一些行需要根据内容调整高度。 Detail 标签可以有很长的 CSV 值列表,我的尝试如下:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(indexPath.section == 0){
        return 176;
    }
    if(indexPath.section == 1){
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {

            cell.detailTextLabel!.frame.size = CGSize(width: view.bounds.size.width - 115, height: 10000)
            cell.detailTextLabel!.sizeToFit()
            cell.detailTextLabel!.frame.origin.x = view.bounds.size.width - cell.detailTextLabel!.frame.size.width - 15
            return cell.detailTextLabel!.frame.size.height + 20
        }
    }
    return 44;
}

但是我在以下位置收到 EXEC_BAD_ACCESS 错误:

if let cell = tableView.cellForRowAtIndexPath(indexPath)

关于为什么我会收到此错误或解决此问题的正确方法有什么想法吗?

最佳答案

您正试图获取一个尚不存在的单元格。 cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell? 返回的单元格是您在 tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell。因此,您试图通过获取单元格(同样,尚不存在)来计算尚不存在的单元格的高度。看起来你想要的是制作一个假的占位符单元格的原型(prototype),你可以设置它来获得真实单元格的高度。为此,您可以将单个单元格作为委托(delegate)/数据源类的属性,设置该单元格一次,然后返回其高度,就像您在高度方法中尝试做的那样。

关于ios - 快速动态的 UITableView 行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060389/

相关文章:

iphone - 如何像 Android 一样在 iPhone 中创建后台服务?

ios - Swift 语言中的 JSON

swift - 如何在退出前终止进程?

ios - 如何使用自动布局在自定义 UITableViewCell 中动态设置 UITextView 高度

iphone - 如何从uitableviewcell中的uibutton加载弹出窗口

ios - UITableView 由于可重用单元而混合值

android - “setup”已被弃用,不应使用。使用购买配置

objective-c - 为什么使用 AFNetworking 而不是 dispatch_async?

ios - 如何在 NSMutableAttributedString iOS Swift 3 中设置对齐方式

swift - 将半精度 float (字节)转换为 Swift 中的 float