Silversnail
Silversnail

Reputation: 386

Using multiple custom UITableViewCells

I´m trying to achieve a grouped styled UITableView like the Contact apps detailedView. I want the top most cell to be transparant and have a UISegemtedControl at the bottom.

When I try to create two different types of custom cells, only the first one is loaded even though i use two different cellIdentifiers.

Would appreciate som guidance. Or some good tutorial tips for the same topic.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
    backView.backgroundColor = [UIColor clearColor];
    cell.backgroundView = backView;
    [backView release];
    */

    static NSString *cellIdentifier1 = @"DetailCellStyle1";
    static NSString *cellIdentifier2 = @"DetailCellStyle2";

    if (indexPath.section == 0) {

        // Load from nib
        DetailCellViewController *cell = (DetailCellViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle]
                                        loadNibNamed:@"DetailCellView" 
                                        owner:nil 
                                        options:nil];

            for (id currentObject in topLevelObjects) {
                if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                    cell = (DetailCellViewController *) currentObject;
                    break;
                }
            }
        }

        return cell;
    }
    else  {
        // Load from nib
        DetailCellViewController2 *cell = (DetailCellViewController2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle]
                                        loadNibNamed:@"DetailCellView" 
                                        owner:nil 
                                        options:nil];

            for (id currentObject in topLevelObjects) {
                if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                    cell = (DetailCellViewController2 *) currentObject;
                    break;
                }
            }
        }

        return cell;
    }

    return nil;
}

Upvotes: 0

Views: 2341

Answers (2)

omz
omz

Reputation: 53551

You load cell 1 and cell 2 in exactly the same way by taking the first object of type UITableViewCell in the "DetailCellView" nib. Therefore, you're getting the same cell in both cases.

Upvotes: 2

markus
markus

Reputation: 315

Well, to honest I am not sure either, but, the load call only apears to be efficiant, when the cell is nil, maybe try to load the second as a else in of the if(cell == nil) call... because in the end of the code, you are setting it nil again ... so maybe :-)

Upvotes: 0

Related Questions