Reputation: 3477
I need to grab the height of a tableView cell from within the tableView:cellForRowAtIndexPath method.
I set the height of the row in tableView:heightForRowAtIndexPath
to return 200 for example. However in tableView:cellForRowAtIndexPath
, when I do NSLog(@"cell height: %f", cell.contentView.frame.size.height);
I don't get 200, I get the default cell height of 44.0. The size of the cell however is visually correct in the simulator. There must be a way to get the size that is specified in heightForRowAtIndexPath
directly without me having to store it in a separate variable - no?
Upvotes: 0
Views: 702
Reputation: 3389
You could just call within the tableView:cellForRowAtIndexPath
.
Here's an example:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
}
Upvotes: 1
Reputation: 539685
The tableView:heightForRowAtIndexPath:
is used by the table view only to determine the vertical position of each cell in the table view, the size of the scroll indicator and such things.
But in tableView:cellForRowAtIndexPath:
you are responsible for creating a cell with the same height, either by setting the frame
or by defining the cell with correct height in a NIB/Storyboard file.
These two methods are independent. There is no magic that cells are created with the height of
tableView:heightForRowAtIndexPath:
. If you create a larger cell, then this cell will just overlap with the next cell in the table view.
So you can call your own tableView:heightForRowAtIndexPath:
method (and I just see that @Larcus94 has suggested exactly this).
But if the height is the same for all cells in the table, a separate constant would be more effective.
Upvotes: 1