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, ie, 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.
may be the only solution is make a variable into the .h file like
@property (nonatomic) int height
initialise it into the viewDidLoad like
self.height = 40;
and then return this variable into the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.height;
}
then in your tableView: cellForRowAtIndexPath:
update this variable like your new height and then reload the [self.yourTable reloadData]
in your viewDidAppear
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.