开发者

how to get a UITableView row height to auto-size to the size of the UITableViewCell?

How to get a UITableView row height to auto-size to the size of the UITa开发者_如何学运维bleViewCell?

So assuming I'm creating the UITableViewCell in Interface Builder, and it height is more than the standard size, how can I get the UITableView row height to autosize to it? (i.e. as opposed to manually having to measure the height in interface builder and than programmatically setting it)


http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ is a great tutorial for this.

The main bit is

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  // Get the text so we can measure it
  NSString *text = [items objectAtIndex:[indexPath row]];
  // Get a CGSize for the width and, effectively, unlimited height
  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  // Get the size of the text given the CGSize we just made as a constraint
  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  // Get the height of our measurement, with a minimum of 44 (standard cell size)
  CGFloat height = MAX(size.height, 44.0f);
  // return the height, with a bit of extra padding in
  return height + (CELL_CONTENT_MARGIN * 2);
}


If all the cells are the same, set the rowHeight property on UITableView to the size of your cell. If they're all different based on content, you're going to have to implement -tableView:heightForRowAtIndexPath: and calculate the height for each row based on your data source.


In the xib with your tableview, you can add a cell object and link it to an IBOutlet in your source-code. You won't use it anywhere, you will just use this to get the height of the cell.

Then, in tableView:heightForRowAtIndexPath: you can use that object to get the height. It's not 100% automatic, but at least this saves you the trouble to manually update your source code when you make changes in the cell view in IB.


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return myDummyCellObject.bounds.size.height;
}

If all rows are of the same type (cell) you can programatically set the tableView.rowHeight property instead of implementing the delegate method above. Depends on your scenario.

Oh, and make sure you don't forget to release myDummyCellObject in -dealloc.


self.tableView.estimatedRowHeight = 65.0; // Estimate the height you want
self.tableView.rowHeight = UITableViewAutomaticDimension; // auto change heights for multiple lines cells.

When implement UITableViewDataSource required method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [self.todoArray objectAtIndex:indexPath.row];
    cell.textLabel.numberOfLines = 0; // allow multiple lines showing up

    return cell;
}


As of iOS 8 you have the option to work with self-sizing cells by specifying these options on your table view:

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension

This will work as long as the system can calculate the rows based on existing constraints or content. Take care that if you set automaticDimension then heightForRowAtIndexPath will not be called!


Sometimes with more complex data some cells can be calculated automatically, but others need a specific height calculation logic. In this case you should only set the estimatedRowHeight and then implement cellForRowAtIndexPath with automatic logic for the cells that can work correctly:

// Set the estimated row height in viewDidLoad, but *not* automatic dimension!
override func viewDidLoad() {
    // other viewDidLoad stuff...
    tableView.estimatedRowHeight = 85.0
    tableView.delegate = self
}

// Then implement heightForRowAtIndexPath delegate method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if theAutomaticCalculationWorksForThisRow {
        return UITableView.automaticDimension
    } else {
        // Calculate row height based on custom logic...
        let rowHeight = 85.0
        return rowHeight
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜