Sjaak Rusma
Sjaak Rusma

Reputation: 1434

Set height of a specific UITableViewCell - iOS

I want to know how to set the height of a specific tableview cell. I know there is a delegate method to set the height of each cell when the table is created but I want to set the height of a specific cell.

I have a referencing outlet to this cell:

@property (weak, nonatomic) IBOutlet UITableViewCell *addBillEventPersonCell;

What I want to do is something like:

self.addBillEventPersonCell.height + 300

I dont want to change the height once but more times because the user can add more content to the cell on a button press.

Thanks,

Upvotes: 0

Views: 504

Answers (1)

Sathish Kumar
Sathish Kumar

Reputation: 229

you do not have any other option than this,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == yourRow)
return dynamicHeight;

return height;
}

And use beignUpdates and endUpdates for smoothness.

Edit: Table view will reuse the cells each time, number of cells created is based on the height you are giving in heightForRowAtIndexPath: and it will call for each cell when it is created or reused.

Initially in cellForRowAtIndexPath you will not have any cells in dequeueReusableCellWithIdentifier: method.

First table view will get height for the particular row and cell from cellForRowAtIndexPath:. once you create and return it, table view will render the view and add it in visible cells array. cellForRowAtIndexPath: is called until it fills the cell in its bounds.

Next time when you scroll table view, the cell which is hiding from the screen is added in reusable array.

And the cell which is coming from down of the screen is the cell which is gone out of the screen or table view bounds. So table view needs height dynamically for each cell.

Cell rendering is purely based on the height of each cell and table view bounds.

Calculate the row height dynamically and reload the table view for your issue.

[yourTableView beginUpdates]; 
[yourTableView reloadRowsAtIndexPaths:indexPath withRowAnimation:animation];
[yourTableView endUpdates];

Hope this will help you.

Upvotes: 2

Related Questions