Chuck
Chuck

Reputation: 339

Custom nib UITableViewCell height

I've created a custom UITableViewCell in IB, linked it to the root view controller's property for it, and set it up in CellForRowAtIndexPath. But the height of my drawn cells doesn't match what I setup in IB, advice? Here's some screenshots and the code. alt text

alt text

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *AddressCellIdentifier = @"AddressCellIdent";

    UITableViewCell *thisCell = [tableView dequeueReusableCellWithIdentifier:AddressCellIdentifier];
    if (thisCell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"AddressCell" owner:self options:nil];
        thisCell = addressCell;
        self.addressCell = nil;
    }

    return thisCell ;
}

addressCell is a @property (nonatomic, assign) IBOutlet UITableViewCell *addressCell;, and is linked up in IB to the file's owner (the table view controller).

I'm using the example from Apple's table view programming guide.

Upvotes: 9

Views: 8709

Answers (6)

MQoder
MQoder

Reputation: 731

For people who are still interested in an alternative answer and dont want to implement heightForRowAtIndexPath, good news :).

You can set the row height in de xib like this:

enter image description here

This is not enough. In fact this will be totally ignored in runtime. The solution is to set the incentric size by setting top and bottom constraints of content of the cell. The content will push the top and bottom of its container which will result in a higher cell :).

enter image description here

Upvotes: 0

Fostah
Fostah

Reputation: 11816

This is what I do when creating the table view to ensure the row height matches the row height defined in the cell's nib:

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]
                      initWithFrame:self.view.bounds
                      style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;

        [_tableView
         registerNib:[UINib nibWithNibName:@"<CELL NIB NAME>" bundle:nil]
         forCellReuseIdentifier:kCellIdentifier];

        // Get the cell's root view and set the table's 
        // rowHeight to the root cell's height.
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"<CELL NIB NAME>"
                                                     owner:self
                                                   options:nil];
        UIView *cellView = (UIView *)nib[0];
        if (cellView) {
            _tableView.rowHeight = cellView.bounds.size.height;
        }        
    }
    return _tableView;
}

I hope this helps.

Upvotes: 2

mike
mike

Reputation: 71

There are two places in the IB where you need to set the row height. First the custom row height for the cell itself. Click on the cell you want to resize, then click on the Size inspector (ruler) in the Utilities window on the right. Set your row height at the top under the Table View Cell section. Click the Custom checkbox. Then click on your Table View in the Document Outline window on the left. Go back to the size inspector in Utilities window on the right and set the Row height for the entire table to the desired height.
With this implementation you don't need to add any code to your tableviewcontroller.

Upvotes: 7

nikolsky
nikolsky

Reputation: 299

I'm using the following snippet. Height right from the nib file.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row == 0) {
      return self.cell0.contentView.bounds.size.height;
   }
   if (indexPath.row == 1) {
      return self.cell1.contentView.bounds.size.height;
   }
   if (indexPath.row == 2) {
      return self.cell2.contentView.bounds.size.height;
   }

   return 44.0;
}

Do not forget to load you cells in - (void)viewDidLoad

[[NSBundle mainBundle] loadNibNamed:@"YourView" owner:self options:nil];

Upvotes: 2

Sixten Otto
Sixten Otto

Reputation: 14816

The delegate's -tableView:heightForRowAtIndexPath: method is one approach, as WrightsCS says. Another option, if all of the rows will be the same height, is to set the rowHeight property of the table view itself. (The former has the advantage of letting you return arbitrary values for each row.)

Upvotes: 5

WrightsCS
WrightsCS

Reputation: 50727

You can adjust the height of the cell by using:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat result;
    result = 120.0f;
    return result;
}

This will work with custom cells.

Upvotes: 5

Related Questions