0

Is there an opposite method to dequeueReusableCell? I am dequeuing cells for cell height computation and after it computes its height I want to recycle that cell so it can be reused for display.

Or should I just instantiate an object per cell type, store it in a property, and the use those instead?

shelll
  • 3,234
  • 3
  • 33
  • 67
  • No other method you have to dequeue cell. – dahiya_boy Feb 06 '17 at 13:28
  • To get you right: You dequeue a cell from the UITableView, in order to set it up and then calculate it's height, and then you throw it away? Or do you call `cellForRow` (which internally calls `dequeue...`), calculate the height and then throw it way? And instead of throwing it away, you want the table to reuse it (so the next time you call `dequeueReusableCell` you will get back one of those? – Andreas Oetjen Feb 06 '17 at 13:37
  • @AndreasOetjen I am calling `dequeueReusableCell(withIdentifier: "xyz")` and then I would like to recycle it, not just throw it away. – shelll Feb 06 '17 at 13:41
  • @shelll If you call `dequeue...`, you will only get back cells that you already have created some time before (in `cellForRow...`). These cells will not be available for further dequeueing, and there is no way to tell the table to enqueue them again. What you could do (see my answer below)... – Andreas Oetjen Feb 06 '17 at 13:50

2 Answers2

0

You could create your own stack / queue, and then put all cells that you dequeue manually (e.g. outside of cellForRow..) into this queue.

Inside of the cellForRow... method, you would then first use the table views dequeue-method, and if this returns nil, you would use your own queue to retrieve reusable cells. Only if none of them returns something, you then would create new cells.

But remember the the table view's dequeue-method only returns cells that you already have created before in cellForRow!

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • Magically it never returns nil in my case on iOS 10, so maybe something has changed over the years. I will end-up with a dictionary with cellId -> cell and instantiate one cell for every id in constructor. Adding own flow into cells recycling is path to hell... – shelll Feb 06 '17 at 14:09
  • Depends on which `dequeue`-Method you call, it will create one for you. Nevertheless, you typically would not implement your app this way to create cells in order to calculate the height, because this is not very performant, expecially for large tables. – Andreas Oetjen Feb 06 '17 at 14:36
  • The `dequeue` method without an `indexPath` should not create a cell, it never created one in older iOS versions. Now both of the `dequeue` methods return non-nil values in my case on iOS 10. Dynamic height cells are huge pain on iOS in general. Automatic height computation is bullshit and slow, estimated height is even bigger shit (not reliable when you need precise scrolling value and causes jumping in some cases). The only robust ways is to compute the height with real cells and then cache the values. And you cannot insert more then 10-20 cells with dynamic height at once... – shelll Feb 06 '17 at 14:46
0

This is still not possible in iOS 10. I will update the answer if this is available in the future.

shelll
  • 3,234
  • 3
  • 33
  • 67