简体   繁体   中英

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

I have a dynamic table view inside a static table view. I am not able to change the cell height according to the cell content. I have tried various methods like

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
}

I have tried setting UITableView.automaticDiemension , but it is not adjusting to the text view. It is getting larger. If I try tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell I am getting only the contents of two cells and rest are not getting the text value.

The current UI screenshot is: UI screenshot

I have set the following table view config in viewDidLoad() :

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

When you called the func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ,you must set cellData again but get data with cell. Then calculate the height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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