Reputation: 1
Well, one might ask why should you do that ?
I have a collection view inside some view where the user can select a cell. Then this selection is saved and later, when the user wants to enter that view again, he should be able to see his previous saved selection.
If his selection was at the end of the collection view, I will not be able to load the collection view with his previous selection, because:
for cell in collectionView.visibleCells()
// find and select previous cell
Will not loop through all cells, specifically the one the user chose (that does not exist yet).
Is there a solution to this problem?
Upvotes: 0
Views: 789
Reputation: 1
Well after a lot of searching, what works for me is to set a local variable say: selectedCell
, then when I want to show a previous user selected cell, i will just set the variable , and on :
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
if(indexPath.row==selectedCell)
}
I am checking if its already selected and mark it . I could not find any other clever way of doing so automatically .
Upvotes: 0
Reputation: 1484
You can remember the exact index paths of selected rows (using indexPathsForSelectedRows
), but this is still not safe if your dataSource
set changes/updates and the remembered indexes may become invalid with new/different set of data. Thus you should remember selection of “real world” objects (where you're filling specific cell data, like title etc.) for example by remembering object identifiers of your choice so you'll be able to re-map and re-select rows in case of reload with different data. The management of objects selection is generally completely in your hands.
Upvotes: 1