ios - 如何更改 iOS 中内 TableView 单元格的单元格高度?

标签 ios swift uitableview

我在静态 TableView 中有一个动态 TableView 。我无法根据单元格内容更改单元格高度。我尝试了各种方法,例如

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (ProfileData.profileViewType == .favourite) {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as? ProfileCell {  // gives EXC_BAD_ACCESS in SimpleCheckbox library
            cell.favouritesTitleLabel.numberOfLines = 0
            cell.favouritesTitleLabel.sizeToFit()
            print(cell.favouritesTitleLabel.text)
            let height = cell.favouritesTitleLabel.text!.height(withConstrainedWidth: cell.frame.width - 64, font: UIFont(name: "Roboto", size: 17.0)!)
            print(height + 16)
            return height + 16
        }
    }
    return UITableView.automaticDimension
}

我已经尝试设置 UITableView.automaticDiemension,但它没有调整到 TextView 。它越来越大。如果我尝试 tableView.dequeueReusableCell(withIdentifier: "profileCell") 作为! ProfileCell 我只获取了两个单元格的内容,其余的没有获取文本值。

目前的UI截图是: UI screenshot

我在 viewDidLoad() 中设置了下 TableView 配置:

profileTableView = ProfileSectionTableView()
profileTableView.profileDelegate = self
profileSectionTableView.delegate = profileTableView
profileSectionTableView.dataSource = profileTableView
profileSectionTableView.rowHeight = UITableView.automaticDimension
profileSectionTableView.estimatedRowHeight = 44

最佳答案

当您调用func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)时,您必须再次设置cellData,但获取单元格数据。然后计算高度。

关于ios - 如何更改 iOS 中内 TableView 单元格的单元格高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54626864/

相关文章:

ios - 如何为 Swift iOS 编程配置 Uncrustify for Allman 风格?

ios - ios推送通知问题

iOS swift : Parse Invalid Session Token Error

ios - 将 UILabel 文本与特定字符对齐

ios - 为 View 中的所有标签设置自定义字体

ios - 如何让 UIButton 只出现在最后一个单元格中?

ios - 如何将登录模块实现为我的 iOS 框架的一部分

ios - 如何在运行时通过 Selector 在 Swift 中调用泛型函数?

iphone - 调用 [tableView reloadData] 恢复帧变化

具有约束的 iOS 自动布局。一些 TableViewCell 正在屏幕外炫耀