Search code examples
iosios6uitableviewnib

Obtain row height dynamically in UITableViewCell with multiple nibs


I have a UITableView with the UITableViewCell loaded from different Nib files when the user taps or double taps a cell i.e there are three different Nib files of varying height, one each for normal, tap and double tap.

How do I identify the loaded nib within heightForRowAtIndexPath: for dynamically setting the row height?


Solution

  • You can dequeue the cell in heightForForAtIndexPath and check it's height:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellId = @"Cell";//set cell ID here based on your view controller's logic
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellId];
        return cell.bounds.size.height;
    }
    

    You can cache the dequeued cells in a dictionary if you're concerned about performance. There is a more detailed discussion of this approach in Retrieve custom prototype cell height from storyboard (contrary to what the title might imply, the use of storyboard is not required).