Table View Cell Row Height doesnt work

前端 未结 8 1046
故里飘歌
故里飘歌 2021-02-05 03:39

I have tried to hard code the row height in the table view cell. After running the program it looks like only one line. I suspect it is because of the height of the row

8条回答
  •  深忆病人
    2021-02-05 04:03

    Ensure following steps to make, auto dimension effective for cell/row height layout. (Here is sample code for UILabel and how to set content specific height for table cell.)

    • Assign and implement dataSource and delegate
    • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
    • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

    -

    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Don't forget to set dataSource and delegate for table
        table.dataSource = self
        table.delegate = self
    
        // Set automatic dimensions for row height
        // Swift 4.2 onwards
        table.rowHeight = UITableView.automaticDimension
        table.estimatedRowHeight = UITableView.automaticDimension
    
    
        // Swift 4.1 and below
        table.rowHeight = UITableViewAutomaticDimension
        table.estimatedRowHeight = UITableViewAutomaticDimension
    
    }
    
    
    
    // UITableViewAutomaticDimension calculates height of label contents/text
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // Swift 4.2 onwards
        return UITableView.automaticDimension
    
        // Swift 4.1 and below
        return UITableViewAutomaticDimension
    }
    

    For label instance in UITableviewCell

    • Set number of lines = 0 (& line break mode = truncate tail)
    • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
    • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

    enter image description here

提交回复
热议问题