简体   繁体   English

多个tableview单元格内的collectionview,如何处理选择?

[英]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,我有一个 tableview,包括 10 个单元格..每个tableview 单元格里面都有一个 collectionview,

I handle each collectionView's selection's style change by overriding isSelected this code我通过覆盖isSelected这段代码来处理每个 collectionView 的选择的样式更改

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 )它在这一点上工作..但是当我滚动 tableview 时,我看到选择样式 (.red) 也应用于其他单元格中的不同集合视图(当然是因为可重用的单元格)

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首先你看到 collectionview 里面有一个选定的项目在此处输入图片说明

when i scroll, i find another dequeued collectionview, with the same selection !, the selection was only at indexpath row 1 not row 5当我滚动时,我发现另一个出列的 collectionview,具有相同的选择!,选择仅在 indexpath 第 1 行而不是第 5 行在此处输入图片说明

What I have done is added a variable named selectedIndex in the modelData.我所做的是在模型数据中添加了一个名为 selectedIndex 的变量。

Check the condition in cellForRowAt and modify the selectedIndex variable on didSelect method检查 cellForRowAt 中的条件并修改 didSelect 方法上的 selectedIndex 变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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