How get contentView width of UITableViewCell?
If I'm not mistaken, heightForRowAtIndexPath: is called either before or right after a cell is initialized, so there's no time for when it resizes.
This should work for you:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%f", cell.contentView.frame.size.width);
}
Unfortunately, this is called after heightForRowAtIndexPath. The only way I've found to adjust the height based on a non-constant width is to set an instance variable for width the first time this method is called, then reload the table view and use the width instance variable inside the heightForRowAtIndexPath.
Alexander
Updated on June 18, 2022Comments
-
Alexander almost 2 years
I need to make custom cell in UITableViewStyleGrouped table.It must look as UITableViewCellStyleValue1 if textLabel and detailTextLabel are showed without truncation, or as UITableViewCellStyleSubtitle (but with detailTextLabel width=contentView.frame.size.width and UITextAlignmentRight) if in Value1 style one of labels is truncated.
I need to know a width of cell.contentView in method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
to decide what height should the cell be. I compare cell.contentView.width and sum of labels widths (i get them using methodsizeThatFits:
) to decide it.So,How to get correct cell.contentView.frame.size.width before the cell creation?
UPD: some code to clarify the question.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat cellHeight = 44; CGSize textSize = [[arrayOfTextData objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake( 1000, 100) lineBreakMode:UILineBreakModeCharacterWrap]; CGSize detailedTextSize = [[arrayOfDetailTextData objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(1000, 100) lineBreakMode:UILineBreakModeCharacterWrap]; if (textSize.width + detailedTextSize.width > /* I need here cell.contentView.frame.size.width*/ 300) { cellHeight = textSize.height + detailedTextSize.height; } return cellHeight; }
UPD2: The problem is that when I create cell the cell.contentView.frame.size.width equal cell.frame.size.width and equal 320, but after cell appeared cell.contentView.frame.size.width changes in dependence of tableView width and tableView style.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tmpCell"; CustomTableViewCellValue1OrSubtitle *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CustomTableViewCellValue1OrSubtitle alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } //--Configure the cell cell.textLabel.text = [arrayOfTextData objectAtIndex:indexPath]; cell.detailTextLabel.text = [arrayOfDetailTextData objectAtIndex:indexPath.row]; [cell layoutIfNeeded]; CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode]; CGSize detailedTextSize = [cell.detailTextLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode]; CGFloat cellHeight = cell.contentView.frame.size.height; NSLog(@"contentView.frame.size.width = %f",cell.contentView.frame.size.width); //out: contentView.frame.size.width = 320.0 //Always print 320, even if I set Simulator on iPad if (textSize.width + detailedTextSize.width > cell.contentView.frame.size.width) { cellHeight = textSize.height + detailedTextSize.height; } return cellHeight; }
-
Alexander over 12 yearsI didn't understand what you mean. I need not dynamic cell width. I want to know cell.contentView width to understand:Do the labels fit for cell without truncation? If not then I want to return in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
other height. -
Alexander over 12 yearsthis code at first out 320 for iPhone, and 750 for iPad, but real contentView width is 300 for iPhone and 698 for iPad (for portrait,width of the screen) and after I scroll table up and down it out right values
-
Alexander over 12 yearsYou right at some point,but it is strange to reload table from the delegate,which responsible for that reloading ;) And I'll have to reload table every time it want to show new cell,which haven't shown yet.
-
simonthumper over 8 yearsThis is a disgusting solution, having to reload your whole table view to calculate this is really not acceptable!