Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read a UITableViewCell (subclass)'s height from a NIB?

Tags:

ios

I have a UITableViewCell (really a subclass) defined in a NIB. In the NIB I've set the frame height to 183, I also set the "Row Height" to 183 and ticked off custom.

In my original question stuff all went wrong here. It looked like I was getting a wrong height back, and the cell loaded at the wrong height and later resized to the right height making a nasty visual glitch.

In reality I was doing some stupid stuff (and always returning the wrong height), and it was too late at night for me to figure it out, so I asked SO for help and went to sleep. The sleep (+coffee) made everything clear.

So for the record, if you load your custom UITableViewCell from a NIB you can look at frame.size.height and the right number comes back.

If you return the wrong number from tableView:heightForRowAtIndexPath: you can get this glitch, at least if the errant cell is the last one in the table (I expect the problem will manifest in another way for other cells)

So I've solved my own problem, and hopefully left enough info for anyone else who hits this. If anyone thinks this ought to have an official answer rest assured I plan on accepting the best looking one in a day or 3.

like image 460
Stripes Avatar asked Nov 22 '11 09:11

Stripes


1 Answers

You can use this example code to get cell height from NIB

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return [[[[NSBundle mainBundle] loadNibNamed:@"ELConvCurListViewCell" owner:self options:nil] objectAtIndex:0] bounds].size.height;
}

Update: You can get and assign height of NIB once time in the viewDidLoad to the cellHeight variable and in the heightForRowAtIndexPath use return cellHeight

like image 77
Aznix Avatar answered Mar 14 '23 21:03

Aznix