Storyboard static cells: dequeueReusableCellWithIdentifier returns nil

前端 未结 5 821
小鲜肉
小鲜肉 2020-11-30 03:55

Using storyboard, static cells, in cellForRowAtIndexPath: the line

UITableViewCell *cell = 
   [tableView dequeueReusableCellWithIdentifier:Cell         


        
相关标签:
5条回答
  • 2020-11-30 04:34

    The solution was to use prototype cells rather than static cells. I am still giving the check to @jrturton as he was the first who got me the idea.

    Another interesting error I just solved: with prototype cells of type "Custom", if you try to fill cell.textLabel with text, it will just automatically work, but all your other subviews behave very strangely. I just use my custom label now, and everything works fine.

    Cheers, thanks everyone for helping out.

    0 讨论(0)
  • Different to the answer above,

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    will not work. But the mentioned method to create an outlet to the cell itself is working.

    It is also possible to place views such as UIButtons or UITextFields on the cell and have outlets for those as well.

    Both methods can also be used in combination. E.g. set the cell.textLabel.text for a particular cell and have another control which will be accessed from the controls outlet.

    0 讨论(0)
  • 2020-11-30 04:50

    In the storyboard, the Static Cells CAN'T implement the Methods in the <UITableViewDataSource> protocol.

    So you could use the methods which ones are include in <UITableViewDelegate>.

    0 讨论(0)
  • 2020-11-30 04:54

    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.

    0 讨论(0)
  • 2020-11-30 05:01

    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.

    0 讨论(0)
提交回复
热议问题