How to set a UITableView static cell to a custom height?
Asked Answered
B

3

9

I have a UITableView with static cells and I am trying to change the height of the cell when it meets a condition. The conditions are being met but the cell heights won't change.

The code I am using is

[self.tableView setRowHeight:10];

And

self.tableView rowHeight = 10;

Neither of these will change the row height of the cells Is there another way to change them?

Bobbie answered 23/6, 2012 at 20:42 Comment(1)
possible duplicate of Different height for alternative cell in UITableViewUpsydaisy
C
18

You need to implement the following delegate-method in your tableview's .m:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return customTableCellHeight;
}

And then change the value of customTableCellHeight:

if (condition) {
    customTableCellHeight = 80; // or whatever
}

When your table cell height needs to change (like when the condition changes, you need to call

 [self.tableView reloadData]; //This calls heightForRowAtIndexPath for all cells
Cicily answered 23/6, 2012 at 20:46 Comment(2)
It doesn't need the reloadData call.Illuse
You need to call reloadData not inside heightForRowAtIndexPath, but outside of it, when the condition changes. This will cause the recalculation of heights and will allow dynamic sizing of cells.Paulpaula
S
1

you can use this for all row of tableview

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      return 100
    }
Scepter answered 9/10, 2015 at 3:5 Comment(0)
L
0

You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view

Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell

Liquate answered 19/2, 2016 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.