简体   繁体   中英

UICollectionView showing selected cells

i have a tableview with form fields, and there is a row that use segue to call an UICollectionView , everything is works except that i can't keep the selected cells (the visual effect that i show when the cell is selected) after go back to the tableview .

I mean, i selected my UICollectionView cells, after that i go back to the form, but if i need to again to my UICollectionView Cells, to deselect o select more cells before submit the data, once the UICollectionView appear my previous selection are there (i print the array, and i see the values) but i cant see the effect that i did for selected cells.

How i can keep the effect for selected cells if I'm going back to select again o deselect cells?

One thing to realize is that this is not going to happen by itself. When you segue back to the table view and the form fields, the UICollectionView is completely destroyed . The next time you show the collection view, it is a new and different collection view.

So, if you want to maintain a knowledge of what the selection was in the collection view, you are going to have to maintain it yourself , deliberately, storing it somewhere when you know that the collection view is being destroyed.

That way, the next time you show your collection view, even though that will be a completely new and different collection view, you can pass the knowledge of what the selection was to that collection view as you create and show it. The user will have the illusion of "returning" to the collection view, in the same state, but in fact you will have saved and restored its state.

It is then just a matter of reflecting the selected state in the collection view's display of its items. To do that, you need to configure your model so that when cellForItemAtIndexPath: is called, each cell looks selected if that item is supposed to be selected.

Every time you use segue to call an UICollectionView a new instance of UICollectionView is created and hence states of selected cells doesn't get restored.

I believe u must have taken an array of index paths of selected indexes to show the visual changes in the cell.

Make your tableView class as delegate of UICollectionView. Using its delegate methods return the selected index array to tableView class .And before pushing to UICollectionView send the same array of index paths back to the UICollectionView. Hope it helps.. Happy Coding.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM