Rahmat Hidayat
Rahmat Hidayat

Reputation: 123

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
}

Upvotes: 10

Views: 38267

Answers (5)

Beetroot
Beetroot

Reputation: 111

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var cellHeight:CGFloat = CGFloat()

    if indexPath.row % 2 == 0 {
        cellHeight = 20
    }
    else if indexPath.row % 2 != 0 {
        cellHeight = 50
    }
    return cellHeight
}

Upvotes: 6

Vishal Vaghasiya
Vishal Vaghasiya

Reputation: 4901

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var height:CGFloat = CGFloat()
        if indexPath.row == 0 {
            height = 80
        }
        else if indexPath.row == 1 {
            height = self.view.frame.size.height - 44 - 64 // 44 is a tab bar height and 64 is navigationbar height.
            print(height)
        }
        return height
    }

Upvotes: 1

Zayne ZH
Zayne ZH

Reputation: 406

You can try using autolayout to create a height constrait and then connect the constraint to an outlet. Then set the constant for that constraint,

var y: Float
if x==0{ //some condition
    y = 10
}else if x==1{ //some condition
    y = 20
}
cell.viewMainHeightConstraint.constant = y 
cell.view.layoutIfNeeded() 

Put this in your cellForRowAtIndexPath

Edit : I misinterpreted the question as asking for another view within the cellView, if so the delegate method heightForRowAtIndexPath is indeed correct as stated in the above answer.

An example:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
    if x == 0{
        return 100.0 //Choose your custom row height
    } else {
        return 50
    }
}

Upvotes: 4

JFAP
JFAP

Reputation: 3717

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.

Upvotes: 6

i_am_jorf
i_am_jorf

Reputation: 54600

TableView cells have their height set by the table view.

You need to implement UITableViewDelegate tableView:heightForRowAtIndexPath:.

Upvotes: 8

Related Questions