views:

106

answers:

1

I am trying to determine the size of a UITableCellView. The reason being that I am using one class for different orientations and devices.

The cell contains one subview that is supposed to fill the entire cell. Right know I'm doing this in the UITableViewCell's init method:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
     //iPad
     subv = [[OrbitView alloc] initWithFrame:CGRectMake(52, 5, 660, 420) ];
}else{
     //iPhone
     subv = [[OrbitView alloc] initWithFrame:CGRectMake(15, 5, 290, 200) ];
}

Clearly, there must be a better way of doing this, without the magic numbers. How/Where should I set the frame of the subview in the UITableViewCell so that it fills the entire UITableViewCell?

+1  A: 

Use the dimensions of the table view that the cell will go in. Set the autoresizingMask to flexible width to handle rotation and accessory views. This code assumes you have set the rowHeight of the table, but you could use a fraction of the screen height instead of testing the device type.

-(CGRect) cellFrameForTableView:(UITableView *)inTable {
  CGRect result = [inTable frame];

  result.origin = CGPointZero;
  result.size.height = [inTable rowHeight];

  return result;
}

Add your custom view to the cell content, not the cell, if you want auto sizing to adjust your view to leave room for cell extras like accessory views. In that case things like edit controls will adjust your custom view along with the content view.

In the past I have had trouble with flexible height views in cells, so I would go with flexible bottom margin instead.

drawnonward