Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

iphone - Storyboard static cells: dequeueReusableCellWithIdentifier returns nil

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...