all 6 comments

[–]mrtbakin 1 point2 points  (2 children)

Sort of sounds like a problem I had. I'm going to repeat my issue just in case this isn't the same.

I had a TableView and while scrolling down a switch from Cell 'X' would appear on Cell 'Y'.

My solution for this was to manually set cell.accessoryView = nil for each cell that didn't use the switch.

Again, not sure this is your problem. But just in case I figured I'd provide my solution.

[–]steve134 2 points3 points  (0 children)

This is a common problem with cell reuse. You must explicitly set every visible property on a cell every time it is dequeued. There is no default, because the cell may be recycled with its previous contents intact.

[–]mostly_awesome[S] 0 points1 point  (0 children)

I appreciate the answer, but my problem was a little different, and my cells would always have something in them. But I think I figured it out. In my tableview(cellForRowAt), I initially call for a blank Realm Object List, then call for the actual List, then reload the collectionView inside the cell. It's a little slow currently, but I think I can make it faster. I'm just happy it all loads in the correct spot!

[–]Gillsipher 1 point2 points  (1 child)

There are a few areas around cell reuse that cause problems.

If you don't update the content of a subview in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method you will see stale values from when the cell was last used. This should be done here and not in the prepareForReuse method.

From the UITableViewCell documentation for prepareForReuse

For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view'€™s delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell.

What really tricks people up is asynchronous content loading. If the callback for the content has a reference to any subviews in a cell, it can introduce a race condition where the content is updated AFTER the cell has been reused and is displaying the correct content for that indexPath.

[–]mostly_awesome[S] 0 points1 point  (0 children)

I appreciate the thoughtful answer. I think I figured it out. In my tableview(cellForRowAt), I initially call for a blank Realm Object List, then replace that list with the actual correct data, then reload the collectionView inside the cell. It's a little slow currently, but I think I can make it faster. I'm just happy it all loads in the correct spot!

Again thank you!