-2

I am just wondering is there a way to have two different cell identifiers in one TableViewController Class? E.g this is my first cell identifier code...

static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

I'm trying to create another cell Identifier, however whenever I do I get an error saying I have a duplicate of code, even when I change the name of the cell identifier. Any help would be great!

Mind Pixel
  • 816
  • 7
  • 16
  • Post the code giving you the problem because you can have dozens of cell identifiers in one table view if you needed them. – rmaddy Apr 24 '14 at 05:57
  • No I only get the error when I try and create another cell identifier, I want to know if there's a way of creating 2 cell identifiers, without getting the duplicate code error. – user3513715 Apr 24 '14 at 05:59
  • Again, post the code giving you the problem. – rmaddy Apr 24 '14 at 05:59
  • Maybe you create same object of NSString. Please post code so other can help you. – ChintaN -Maddy- Ramani Apr 24 '14 at 06:01
  • http://i.imgur.com/1CdphBv.png I know that I have the same code twice. – user3513715 Apr 24 '14 at 06:04
  • 2
    Please don't post images of your code. It's much better to update your question with the actual code. Anyway, you can't have two `cellForRow:` methods, just one. Use an `if` statement based on the `indexPath` – rmaddy Apr 24 '14 at 06:05
  • 2
    See my answer to another question: http://stackoverflow.com/questions/17711920/calling-two-different-custom-cell-in-one-uitableview-issue/17712144#17712144 – rmaddy Apr 24 '14 at 06:06

1 Answers1

0

Yes! you can create 2 cell identifier in one class. But different custom cell xib and different identifier.

static NSString *CellIdentifier1 = @"CellID1";
    CustomCell1 *cell1 = [table dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];

static NSString *CellIdentifier2 = @"CellID2";
    CustomCell2 *cell2 = [table dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];

//While loading cell 

if(indexPath.row ==0)
{
// cell1 code
}
if(indexPath.row ==1)
{
// cell2 code
}
  • 2
    The code for the cell identifier and the call to `dequeueReusableCell` should be inside the `if` statement, not before. – rmaddy Apr 24 '14 at 06:07