0

I'm having an issue and would like to know if what I am trying to do is even possible.

I have a ViewController that has a UITableView as a subview. I then loaded a custom cell and it worked fine. However, I needed another custom cell in the same tableview - this second cell has a separate nib as well.

Is this possible?

Two different custom cells in on section in a UITableView?

I did try something like this:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    // Space Cell
    if (indexPath.row== 0) {
        CellSpace *cell = (CellSpace *)    [tableViewdequeueReusableCellWithIdentifier:CellIdentifier2];
        return cell;
    }

    // Content cell
    else {
        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        // Configure cell
        return cell;
    }
}

However it seems the else statement is never fired and I normally get a crash saying the method returned nil.

UPDATE

I used the advice and code here and came up with this:

#pragma mark - UITableView Delegate Methods 

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

    return 160;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 22;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *CellIdentifier1 = @"UsernameCell";
    static NSString *CellIdentifier2 = @"PasswordCell";

    if(indexPath.row == 0) {


        BBUsernameRegistrationCell *cell = (BBUsernameRegistrationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if(cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier1 owner:self options:nil];             // set properties
            cell = nib[0];
        }
            // set properties
        return cell;
    }

        // Content cell
    else {
        BBPasswordTableViewCell *cell = (BBPasswordTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if(cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier2 owner:self options:nil];             // set properties
            cell = nib[0];
        }
            // set properties
        return cell;
    }
}

However I only see the first Cell in my tableview. If I switch the cells around the second one loads. But not both at the same time. Does anyone have any idea why?

Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99
  • of course it is possible. have you implemented `- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section` and `- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView` delegate methods? – Neil Galiaskarov Feb 05 '14 at 06:26
  • Yes. Sorry I should have put that in the OP. Number of sections is set to 1 as I only want one section. And numberOfRows in section is 2. The code above does't load the cells from the nib - not really sure how that is done for more than one cell. – Robert J. Clegg Feb 05 '14 at 06:29
  • Try to debug with breakpoints and check if `tableViewdequeueReusableCellWithIdentifier` does not return nil. if so init your cell `cell = [[SamplesCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];` – Neil Galiaskarov Feb 05 '14 at 06:30

3 Answers3

4

Yes, it is possible..

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    // Space Cell
    if (indexPath.row== 0) {
        CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if(cell == nil) {
            cell = [[CellSpace alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
            // set properties
        }
        // set properties
        return cell;
    }

    // Content cell
    else {
        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if(cell == nil) {
            cell = [[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            // set properties
        }
        // set properties
        return cell;
    }
}

in your custom cell, do something like this in initWithStyle method:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];

    for (id currentObj in topLevelObjects ) {
        if ([currentObj isKindOfClass:[CellView class]]) {
        self = (CellView*) currentObj;
        }
    }
    return self;
}
Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
  • This helped me. I managed to get it working. Just deleted all the code and UIViews and started over. I must have been missing something. But it works now. Thanks all – Robert J. Clegg Feb 05 '14 at 07:28
1

Yes it is possible. But before use this dequeueReusableCellWithIdentifier:. Just register your cell and corresponding nib files.

code as..

-(void)initiateCellRegisteration
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";
    [self.tableView registerClass:[ContentCell class] forCellReuseIdentifier:CellIdentifier2];
    [self.tableView registerNib:[UINib nibWithNibName:CellIdentifier2 bundle:nil] forCellReuseIdentifier:CellIdentifier2];


    [self.tableView registerClass:[SpaceCell class] forCellReuseIdentifier:CellIdentifier1];
    [self.tableView registerNib:[UINib nibWithNibName:CellIdentifier1 bundle:nil] forCellReuseIdentifier:CellIdentifier1];


}

Call this method in viewDidLoad

Mani
  • 17,549
  • 13
  • 79
  • 100
0

If you are using the cell nib then you need little tricky way to get your cell nib like

static NSString *CellIdentifier = @"ProductCell";
    IVProductCell *cell = (IVProductCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* objects  = [[NSBundle mainBundle] loadNibNamed:@"IVProductCell" owner:nil options:nil];
        for (id currentObject in objects) {
            if ([currentObject isKindOfClass:[IVProductCell class]]){
                cell = (IVProductCell*) currentObject;
            }
        }
    }
Retro
  • 3,985
  • 2
  • 17
  • 41