Storyboard static cells: dequeueReusableCellWithIdentifier returns nil

16,542

Solution 1

With static content in a table view, you do not implement any of the datasource methods (including tableView:cellForRowAtIndexPath:, so you would never dequeue the cells. There is no dequeuing for static content (that you can get involved in, anyway).

If you want to get a pointer to a particular cell:

  • get it from the table view using cellForRowAtIndexPath::

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
  • have an outlet to the specific cell and customise it directly.

  • Iterate through the cells and check the reuseIdentifier property to get the cell you are interested in.

Any of these things can be done in viewWillAppear or similar.

If you want to have completely different content in your cells to that found on the storyboard then static cells probably aren't the right choice. You should use dynamic prototypes (note you can have multiple prototypes in the storyboard) with the traditional data source methods instead.

Solution 2

You can still use dataSource/delegate methods of static UITableView, you just don't have to create new cells.

If you want to modify cells with dataSource methods, inside cellForRowAtIndexPath: :

UITableViewCell * cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

and then start modifying cell.

Share:
16,542

Related videos on Youtube

Mundi
Author by

Mundi

I develop for iOS et al. Harvard grad. Ebay alumn. Pharma expert. Blockchain evangelist.

Updated on June 07, 2022

Comments

  • Mundi
    Mundi almost 2 years

    Using storyboard, static cells, in cellForRowAtIndexPath: the line

    UITableViewCell *cell = 
       [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    always returns nil.

    I have checked the following:

    • Identifier of the cell is correctly set in IB/Storyboard and I use the same identifier in code. I verified this many times.
    • I have not instantiated the view controller elsewhere (which was the problem in this stackoverflow question).

    My view controller is a subclass of UITableViewController, of course, wrapped into an ad hoc navigation controller in storyboard. Suspecting that my view controller somehow does not know about the cell identifiers defined in storyboard because it might be another instance, here is the code the "instantiates" it. In prepareForSegue:, I use

    CustomViewController *vc = [[[segue destinationViewController] 
       viewControllers] objectAtIndex:0];
    

    Other customizations of the view controller done here (setting properties etc.) works fine.

    I am using static cells because the number of sections and rows does not change, and each cell contains static text (and other controls or text fields to be edited).

    It seems to me this is a very common task (customize static cells from storyboard in the view controller's datasource methods). What am I doing wrong?

    • borrrden
      borrrden about 12 years
      You will have to show us where you actually handle creating your custom cell. That is the important part.
    • Mundi
      Mundi about 12 years
      I create the custom cell in storyboard. Type "Custom" and its own cell identifier. Does it not work like that?
    • borrrden
      borrrden about 12 years
      I missed the whole static part. Static cells are designed entirely in the storyboard. see part 2 of this excellent tutorial www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-pa‌​rt-2
    • Mundi
      Mundi about 12 years
      I went through that. Why is it returning nil?
    • borrrden
      borrrden about 12 years
      Because you can't use that with static storyboards :). That method is for prototype style.
  • Mundi
    Mundi about 12 years
    Thanks - so for what I want to do it seems I really need to use prototype cells. Could you add that to your answer, so I can check it? Thanks for your clarifications!
  • jrturton
    jrturton about 12 years
    Edited my answer, you didn't actually say what you were trying to do, so hope it helps!
  • Mundi
    Mundi about 12 years
    I am trying to have static title labels and editable text fields in some of the cells, and some other controls in others. I thought you can fill in data dynamically into static cells. The number or position of the cells does not change. I have started doing it with prototype cells, and it is so much work - almost worse than before storyboard!
  • jrturton
    jrturton about 12 years
    Just have an outlet to each editable text field. Easy peasy. I have done this myself for a settings type screen.
  • Mundi
    Mundi about 12 years
    That is an insane amount of work! It has got to be simpler than that. I am filling in the properties of a custom object, so it could be potentially lots of data to be processed, but mostly text in text fields. Plus, this does not work with prototype cells. - Plus I just realize, that the text fields are invisible for some reason... Storyboard nightmare!
  • Mundi
    Mundi about 12 years
    For now, say, 8. Could be 24 in the future. I still do not understand why the cell is coming back as nil with static cells. I do not understand why the text field is not visible in prototype cells.
  • Drux
    Drux almost 11 years
    How about the special case of table views with mixed static and dynamic content as discussed here?
  • Mazen Kasser
    Mazen Kasser about 10 years
    how did you use "Custom" Cell? because dequeueReusableCellWithIdentifier returns nil?
  • Mundi
    Mundi about 10 years
    You can specify "Custom" in Interface Builder.
  • Mazen Kasser
    Mazen Kasser about 10 years
    I did specify Custom in Interface Builder, then I subclassed the UITableViewCell and assigned to it this cell. But it brings empty dynamic cells.
  • Mundi
    Mundi about 10 years
    Sounds like you are not configuring them correctly.