简体   繁体   中英

Two UITableViewCell with different identifier and different height in storyboard

I have two type of cells that I'll be using my UITableView, so I created two prototype cell with different identifier. I manuelly changed the size but when I compile and run, the two cells have same size.

Is there any way to do it through storyboard and without checking every single time

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

I will have around a good 100 cells at least.

UPDATE : in my case I have a tableview with 3 sections, the first one is small, the second and the third one are bigger.

I think your best option is to use that -tableView:heightForRowAtIndexPath: delegate method you listed and return the height you would like.

You could assign tags to each prototype and use an if/else if conditional.

Or if you have a subclassed UITableViewCell for each prototype you could do something like

...
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[CustomCell1 class]]) {
    // return aNumber;
} else if ([cell isKindOfClass:[CustomCell2 class]]) {
    // return aNumber;
} else {
    return aNumber;
}
...

If you know that the sections if what determines the height of the cell, then you can implement it like this:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
    CGFloat result;
    if (indexPath.section == 0) {
        result = 160;
    } else {
        result = 80;
    }
    return result;
}

Depending on the heights that you need of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM