Reputation: 6353
I set height for each UITableViewCell in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
I need to get each UITableViewCell's height in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
because I need to addSubView in different cells.
I've tried to use cell.frame
& cell.bounds
& cell.contentView.frame
, but the height hasn't changed.
Upvotes: 4
Views: 11761
Reputation: 64
As @ilMalvagioDottorProsci says,The value returned by the heightForRowAtIndexPath function do not affect the actual size of the cell.
If you want achieve that each row height is determined by the cell height.I have a trick.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id aKey = [self buildCellCacheKey:indexPath];
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if ([_cellCache objectForKey:aKey]==nil) {
[_cellCache setObject:cell forKey:aKey];
}
return cell.bounds.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// for flexible height of table view cell.in this way,the cell height won't be caculate twice.
id aKey = [self buildCellCacheKey:indexPath];
UITableViewCell *cacheCell = [_cellCache objectForKey:aKey];
if (cacheCell) {
[cacheCell retain];
[_cellCache removeObjectForKey:aKey];
LOGDebug(@"return cache cell [%d]",indexPath.row);
return [cacheCell autorelease];
}
// here is the code you can config your cell..
}
Upvotes: -1
Reputation: 27072
What about UITableView
s method, - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
.
Get the rect like this,
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
Upvotes: 2
Reputation: 1558
I solved this way:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// create object
id myObj = [_arrayEvents objectAtIndex:indexPath.row];
// create static identifier
static NSString * myIdentifier = @"Cell";
// create static UITableViewCell
static UITableViewCell * cell;
// create cell with object
cell = [self buildCell:myIdentifier tableView:tableView obj: myObj];
return cell.contentView.frame.size.height;
}
Upvotes: 2
Reputation: 93
This may be too late, but you can use the delegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect cellSize = cell.frame;
}
Hope it helps anyone, Let me know if I missed out something. :)
Upvotes: 8
Reputation: 1332
They are different stuffs. In heightForRowAtIndexPath: you tell the UITableView how much vertical space it shoud use to show the corresponding cell, but this don't affect the actual size of the cell! So if you want them to match you have to manually set the dimensions.
If you don't want / need to resize the cell frame you should simply store the height in a property of your custom cell class.
Upvotes: 1