简体   繁体   中英

How to set a UITableView static cell to a custom height?

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?

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

you can use this for all row of tableview

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      return 100
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM