Note that there are some explanatory texts on larger screens.

plurals
  1. POAdjust width of UITableView based on UITableViewCell with longest width
    text
    copied!<p>I have a <code>UITableView</code> that serves as a pop up menu and I want to set its width to the width of its widest <code>UITableViewCell</code>. However, I've tried obtaining the width of the cell <code>UILabel</code> / <code>contentView</code> and it always returns 0. I try doing this after I've added to the subview and after the table has been reloaded and still the same result. I've done it within the <code>tableView:cellForRowAtIndexPath:</code> method and outside of it and same result. Is it possible to do this? My code is listed below:</p> <pre><code>- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (tableViewCell == nil) { tableViewCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; } [[tableViewCell textLabel] setFont:[UIFont fontWithName: @"Helvetica-Bold" size: 17]]; [[tableViewCell textLabel] setText: [self.menuItems objectAtIndex:0]]; [[tableViewCell imageView] setImage: [self.menuItems objectAtIndex:1]]; if (tableViewCell.textLabel.frame.size.width &gt; tableView.frame.size.width) { CGRect tableViewFrame = tableView.frame; tableViewFrame.size.width = tableViewCell.textLabel.frame.size.width; tableView.frame = tableViewFrame; } return tableViewCell; } </code></pre> <p>UPDATE: Just to clarify, I'm not interested in changing the width of the TableViewCell while the TableView is reloading. I'd be fine with loading the table, then calculating the appropriate max width, then reloading it again with the new appropriate width.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload