简体   繁体   中英

Collectionview inside multiple tableview cells, how to handle selection?

I have a tableview, consisting say, 10 cells.. Each tableview cell has a collectionview inside it,

I handle each collectionView's selection's style change by overriding isSelected this code

override var isSelected: Bool {
    didSet{
        if(isSelected){
            backgroundColor = .black
        } else {
            backgroundColor = .red
        }
    }
}

It works at this point.. But when i scroll the tableview, i see that selection style (.red) is applied on different collection views that in other cells as well ( because of reuseable cell of course )

I want to keep selection stickied to the collectionview that has a selection inside it, and avoid selection applied on other collectionviews as well

first you see that collectionview has a selected item inside在此处输入图片说明

when i scroll, i find another dequeued collectionview, with the same selection !, the selection was only at indexpath row 1 not row 5在此处输入图片说明

What I have done is added a variable named selectedIndex in the modelData.

Check the condition in cellForRowAt and modify the selectedIndex variable on didSelect method

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