How to get indexPath in cell class?

I'm trying to get the indexPath of a tableView cell in the cell class.

I've got a collectionView inside the table view cells and I'm trying to make label inside the collectionView cell show the indexPath.row of the tableView cell that particular collectionView in.


Currebtly i have

var indexPathForCell: indexPath

in my cell class.


Then in the tableView class i have this in cellForRowAt indexpath

cell.indexpathForCell = indexPath
lbl.text = String(indexPathForCell.row)


If there is "3" or fewer tableView cells this works but if theres more then the 4th row then shows "0" as the indexPathForcell.row, and as I scroll in the collectionView the numbers then chnage from "0" to "3" and even show "1". Each cell then shows a different number as i scroll.

I tried both ways and they kind of worked (I'll use reloadData() in the prepareForReuse method as it seems to be safer).

When just setting labels and/or colours from local data it works fine, but when i try load data from Firebase using the subCat value (indexpathForCell.row) as part of the directory it sets the correct cell to the right label values etc. but it also sets other random cells in the same as well as other collectionViews (in other tableView rows) to the same values and cells that behave like this change as i scroll in the collectionView. Also some rows tend to mirror each other somehwhat. So like if i scroll to the end of the collectionView in tableView Row 4, the collectionView in tableView Row 0 will also have scrolled to the end.


I'm going to see if i can download and store all the data needed before the before the view is called to load so it should work (since it worked with local data). But what is causes that behavior when using the value for Firebase?

It's hard to say why. Presumably the difference is that you're receiving the data asychronously. Maybe your using data in an inconsistent state. Maybe you have an optional that's nil at an unexpected time. Maybe you're updating the table view or a collection view on a background thread. Maybe you need to reload some table view cells explicitly.

Yes, doing this sort of thing while populating the data asynchronously is definitely a more interesting case. By the time the reply from the server comes back, your table view cell and/or collection view cells may have been scrolled off the screen and reused. So if whatever bit of code (closure, method etc.) handling the asynchronous response is holding a reference to a particular cell instance, it may be updating the wrong one by the time it runs.


In that case it's even more important than usual to really grok the MVC (model-view-controller) paradigm. You will need to separate the model from the views in your mind and in the code, and be absolutely crystal clear about how the view gets updated when the model changes, since these changes can happen at any time.


You could look up the Apple sample code LazyTableImages for an example. Yes it's in Objective-C but the principles are the same. There may be better tutorials out there on the web too - the key words to search would be asynchronous updates to your UITableView or UICollectionView data model.

How to get indexPath in cell class?
 
 
Q