繁体   English   中英

如何在 iOS 中更改内部表格视图单元格的单元格高度?

[英]How to change the cell height of inner table view cell in iOS?

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

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 ,但它没有调整到文本视图。 它越来越大。 如果我尝试tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell我只得到两个单元格的内容,其余的没有得到文本值。

当前UI截图为: UI截图

我在viewDidLoad()设置了以下表格视图配置:

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但使用单元格获取数据。 然后计算高度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM