Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewAutomaticDimension not working with custom table cell

there is plenty of question answering the dynamic height for UITableViewCell of UITableView. However i can't solve my problem.

I have this table cell creation code:

class UINoteTabelViewCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }

    func fill(_ note: Note) {
        let view = UINote(withNote: note, atPoint: .zero)
        self.contentView.addSubview(view)
    }
}

This is how i create cell for table view:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotePreview", for: indexPath as IndexPath) as! UINoteTabelViewCell

        cell.fill(notes[indexPath.row] as! Note)
        return cell
    }

Also table view estimatedRowHeight and rowHeight are sets to UITableViewAutomaticDimension

But table view still draws with 44.0 rows height.

I have no idea how to fix it.

PS I can't set fixed estimatedRowHeight because every cell have dynamic height

like image 737
Sergi Garsa Avatar asked Oct 27 '25 14:10

Sergi Garsa


1 Answers

You should give the estimatedRowHeight some default value, say 60. Then the rows will have a default height of 60 but whenever the content needs height more than 60 at that time UITableViewAutomaticDimension will work.

like image 124
Ayush Mahajan Avatar answered Oct 29 '25 04:10

Ayush Mahajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!