I want to adjust the row height of a UITableView according to the cell in that row.
Initially, I tried to use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
However, the problem is, when the table is loading, the calling flow is seemed to be:
First call:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Then call:
(UIViewTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
which means, before my cell is generated, I have to tell the table the height of its row.
However, what I want is exactly the opposite, i.e., after my cell is generated, I tell the table the height of this rows.
Is there any method to achieve this?
Make an array of heights for every row based on the data from tableView: cellForRowAtIndexPath:
and in the tableView heightForRowAtIndexPath:
just take the height value from that array.
Declare in your implementation file:
NSMutableArray *heights;
In viewDidLoad:
initialise it:
heights = [NSMutableArray array];
In tableView: cellForRowAtIndexPath:
set the height for each row:
[heights addObject:[NSNumber numberWithFloat:HEIGHT]];
And in the tableView heightForRowAtIndexPath:
return required height:
return [heights objectAtIndex:indexPath.row];
This should work because tableView: cellForRowAtIndexPath:
is called for every cell and then for every cell tableView heightForRowAtIndexPath:
is called.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments