ios - 静态 UITableView,在 Swift 中创建具有动态高度的单个单元格

标签 ios swift uitableview

我有一个静态 UITableView,它有 12 行,对于 11 行,我知道高度需要是多少。

我有一个 UILabel,它具有位于第 12 行内的动态文本,我如何着手让一个单元格具有基于 UILabel 中的文本的动态高度。

我已经在 viewDidLoad 中尝试了下面的代码,但没有成功。行保持相同的高度。我还为 UILabel

设置了 lines = 0
    tableView.estimatedRowHeight = 100.0
    tableView.rowHeight = UITableViewAutomaticDimension

最佳答案

你试过吗?

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row < 11) {
        // Everything starts at 0, so this covers 0-10
        // Whatever your cell height is
        return <#fixedCellHeight#>
    } else {
        // This is your 12th cell (indexPath.row 11), as we start counting at 0
        return UITableViewAutomaticDimension
    }
}

关于ios - 静态 UITableView,在 Swift 中创建具有动态高度的单个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969347/

相关文章:

iphone - 设置 UITableViewCell 文本的宽度

ios - 为什么我的应用程序在点击播放按钮时停止

swift - @objc'只能应用于类的扩展,从swift调用扩展方法到objective c

iphone - iOS 中当年的天数

uiimageview - 在 IBAction 之前调用 prepareForSegue

Json 解析 Swift 3 Alamofire

iphone - 如何在表格 View 上方创建一个箭头

ios - 在 Swift 中处理单元格重新迭代的问题

ios - 为测试定义常量值,为调试定义另一个常量值

ios - iTunes Store 不显示应用程序的更新按钮