Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set UITableView cell height programmatically in Swift?

How to set view height programmatically? I have this code:

cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)

But that is not working.

UPDATE:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("updateCell", forIndexPath: indexPath) as! UpdateCell
    
    if self.data[indexPath.row] == "b" {
        tbUpdate.rowHeight = 85
        cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)
    }else{
        tbUpdate.rowHeight = 220
        cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 200.0)
    }
    
    return cell
}
like image 421
Rahmat Hidayat Avatar asked Sep 02 '16 01:09

Rahmat Hidayat


Video Answer


2 Answers

TableView cells have their height set by the table view.

You need to implement UITableViewDelegate tableView:heightForRowAtIndexPath:.

like image 136
i_am_jorf Avatar answered Oct 27 '22 23:10

i_am_jorf


first, rowHeight changes the height of all rows in your table. if you want specific height for specific rows, implement tableView:heightForRowAtIndexPath: method. remove tbUpdate.rowHeight in your code first.

like image 32
JFAP Avatar answered Oct 27 '22 23:10

JFAP