I have an application that uses tableviews on every screen. I have plenty of custom cells, each having different height. Right now calculating the height is quite a challenge. I am looking for a generic method that will return the height for each custom cell without using heightForRowAtIndex. The reason I don't want to use heightForRowAtIndex because I have 4 xibs behind one cell and I don't want to mess this metho with if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {}
kind of checks. For the record cell xibs are both iPhone, iPad with english and arabic versions:
cell_en.xib
cell_en~ipad.xib
cell_ar.xib
cell_ar~ipad.xib
Currently, I am trying following;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: { VPCell1 *cell=[VPCell1 cellForTable:tableView withOwner:self]; [tableView setRowHeight:CGRectGetHeight(cell.contentView.frame)]; return cell; } case 1: { VPCell2 *cell=[VPCell2 cellForTable:tableView withOwner:self]; [tableView setRowHeight:CGRectGetHeight(cell.contentView.frame)]; return cell; } case 2: { VPCell3 *cell=[VPCell3 cellForTable:tableView withOwner:self]; [tableView setRowHeight:CGRectGetHeight(cell.contentView.frame)]; return cell; } } }
Here I am using [tableView setRowHeight:CGRectGetHeight(cell.contentView.frame)];
to set row height for each cell.
Turns out setRowHeight method should be used, iff all cells inside a tableview have same height so definitely not a choice here for performance optimization. I am open to ideas to solve this mess.
Thanks!