0


I'm creating a universal iPhone/iPad app and I want to have custom cells on the main view. It's working fine for iPhone, but on iPad dequeueReusableCellWithIdentifier: returns nil instead of cells. Here's some of my code (I replaced my custom cells with normal UITableViewCell since it doesn't work anyway):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        CellIdentifier = @"MainPadCell"; //works fine when replaced with MainPhoneCell
        __weak UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //returns nil

        [cell.textLabel setText:@"Test"];

        return cell; //crash
    }else{
        CellIdentifier = @"MainPhoneCell";
        __weak UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        [cell.textLabel setText:[NSString stringWithFormat:@"Cell %i", indexPath.row]];

        return cell;
    }
}

Weird thing is, that when I replace MainPadCell cell with MainPhoneCell it shows the phone cells (though I don't have any MainPhoneCell reuse identifier in my iPad Storyboard, I have MainPadCell in iPad storyboard and it can't find it).
Thanks for any advice!

haluzak
  • 1,133
  • 3
  • 17
  • 31
  • I already solved the problem, this code works well, thanks everyone for looking and answering. – haluzak Mar 06 '13 at 10:54
  • Checking to see if the user has an iPhone or iPad in the cellForRowAtIndexPath is a really inefficient way of doing things. This method will fire whenever a cell is displayed. Potentially thousands of times. – LJ Wilson Mar 06 '13 at 11:26

1 Answers1

2

The reuse identifier needs to be the same if you are using the same controller for both. You can optionally have more objects (labels, etc) in the iPad TableViewCell and can lay things out differently in the iPad cell, but the subclassed TableViewCell (if you are subclassing it) and the reuse identifier need to be the same for both iPhone and iPad.

Make sure to also connect up the cell, the cell objects (again, if you are subclassing) and the TableView Delegate and Datasource outlets to the VC you have subclassed.

Link to sample project showing how to use the same sublcass and reuse id: https://dl.dropbox.com/u/3660978/UniversalTableView.zip

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • Hi, thanks, but the reuse identifier doesn't need to be the same because I'm using 2 different storyboards and I can set 2 different reuse identifiers and check if I'm on iPad or iPhone as I'm doing. I finally found the problem, it was completely elsewhere and the code I wrote above works perfectly well. Thanks for you time though! – haluzak Mar 06 '13 at 10:54
  • Why would you check for a different reuse identifier? You realize that if you are doing this in cellForRowAtIndexPath, this will fire every time you scroll and display a cell right? I just finished a sample project to show you what I mean about using the same reuse identifier. I updated the answer and provided a link to the zipped project. – LJ Wilson Mar 06 '13 at 11:24
  • Yeah you're right, I was just testing and didn't realise that, thanks for pointing it out :) – haluzak Mar 06 '13 at 11:30
  • @ElJay : link is broken, can you plz update the code in github – Bishal Ghimire Apr 25 '14 at 11:03