MikeTheCoder
MikeTheCoder

Reputation: 963

How can I make a UITableViewController display two different UITableViewCell's, at two different heights?

I have a UITableView controller, UITableView xib, and two UITableViewCell subclasses with there xib's. I dragged out an individual cell for each xib. My first cell's height is 115 and my other cell has the default height. When cellForRowAtIndexPath is called in my controller I have a conditional statement that sets the first row to my bigger cell. Every row after that is set to my other cell. Even thought the bigger cells are being rendered, the default cells are not recognizing the fact that the first cell is bigger than the default, and therefor the default cells are overlapping the bigger cell. I set a distanceLabel outlet at the bottom of my bigger cell to show you what I mean:

enter image description here

Note: The 2.3 miles is part of the first cell.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // load the cell nib
    UINib *nib = [UINib nibWithNibName:@"CustomViewCell" bundle:nil];
    UINib *bigNib = [UINib nibWithNibName:@"BigCell" bundle:nil];

    //Register the nib
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomViewCell"];
    [[self tableView] registerNib:bigNib forCellReuseIdentifier:@"BigCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
        BigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BigCell"];
        NSString *title = [self.frontList objectAtIndex:indexPath.row];
        [[cell nameLabel] setText:title];
        [[cell distanceLabel] setText:@"2.3 miles"];
        return cell;
    } else {

        CustomFrontCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"CustomViewCell"];
        NSString *titles = [self.frontList objectAtIndex:indexPath.row];
        [[cell nameLabel] setText:titles];
        return cell;
    }
}

How can I make these cells render with the correct spacing and height?

Upvotes: 0

Views: 670

Answers (1)

DanSkeel
DanSkeel

Reputation: 4005

You can use UITableViewDelegate method tableView:(UITableView *)tableView heightForRowAtIndexPath: to control height.

Upvotes: 2

Related Questions