1

I have created a static table cell in a .xib, however when it is displayed the cell height set in the xib is not being applied, instead the cell is being displayed with what looks like the default height for cells.

I have set things up as:

1) The table view controller derives from UITableViewController

2) The TVC is not contained within a xib and is created in code in the app delegate and added to a tab bar controller.

3) The TVC is created using initWithNibName:bundle: with the nib name supplied as the name of the xib containing the table view and the static table cell.

4) The TVC has an IBOutlet iVar of type UITableViewCell*

5) cellForRowAtIndexPath returns this iVar.

6) numberOfRowsInSection returns 1, numberOfSectionsInTableView returns 1;

7) The xib contains a table view, which is connected to the File's Owner's view. The File's Owner is set to my custom table view controller class.

8) The UITableViewCell in the xib is connected to the outlet of type UITableViewCell in the File's Owner.

9) THe Table view style in the xib is set to grouped.

10) The size of the Table View Cell shown in the size inspector is N, and this is where the problem is. If I change N manually or by resizing the cell visual representation then this size is not applied when the table is drawn. I have also tried setting the view Frame Rectangle height in the size inspector to match. It doesn't matter what value N is, big or small.

11) The cell's height is not being set anywhere in the TVC, if I do set it using self.tableView.rowHeight then its height does change. But I do not want to set it explicitly like this, I want the height size in the xib to be picked up.

This has been driving me nuts for a few days, and reading several tutorials on table views (including Apple's table view programming guide, especially the section on static cells, whose instructions I followed in the first place to set everything up) has not given me any clues where the problem lies.

TIA

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

3 Answers3

1

Implement heightForRowAtIndexPath and return the height of the UITableViewCell linked to in your IBOutlet.

ikuramedia
  • 6,038
  • 3
  • 28
  • 31
  • Thanks. That works. Is this a workaround or the way you are supposed to use static cells with a .xib - as its not mentioned as a step in the tutorials I've been looking at. – Gruntcakes Jan 16 '12 at 21:15
  • I'm not 100% sure as I mostly use UIStoryboards now which seem to handle static cells much better, but certainly the method is the correct one to control the row height from, and if you're loading your tableviewcell from a XIB in this way, I don't see any better alternative. – ikuramedia Jan 16 '12 at 21:17
  • how would you find out which nib it is? – MJB Jun 06 '12 at 00:34
0

I had the same problem and everyone here and on other threads suggests writing code to fix the problem, which seemed wrong. What's the point of using IB if you have to write code to get the properties right?

The answer turns out to be very simple but took a bit to track down. In IB there are two places that row height can be set (just like there are two places to set it in code). One is on the table, the other is on the cell.

The place I was (incorrectly) setting it was the tableViewCell's "Row Height" property in the Property Inspector. Changing this value had no effect on row height at runtime.

Then I found and set the "Row Height" property of the TableView (one step up the object hierarchy) and viola, the row height is correct at runtime. Once this was working I turned off the override of Row Height on the TableViewCell (uncheck "Custom" on the Row Height property). I was afraid this might be evaluated on each cell, which would kill performance since all my cells are the same height.

Swany
  • 615
  • 8
  • 14
  • 1
    the problem here is that if you have different custom Cells, you set the height for all of them. – MJB Jun 06 '12 at 00:39
0

Do you have - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath defined in your UITableView delegate?

You may want something like:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

Try this post if you're still having trouble try this post: Setting custom UITableViewCells height

Community
  • 1
  • 1
  • 2
    The asker is looking for the XIB to control the height, not have it set programmatically. – ikuramedia Jan 16 '12 at 21:08
  • Thanks but no I don't, but I shouldn't have to? Numerous sources I have read say the cell's height can be set in the xib, if I implement this function I am setting it programatically, which is what I said I don't want to do. – Gruntcakes Jan 16 '12 at 21:09