CollectionView滚动会更改所选单元格。例如,我的collectionView中有26个字母,items [0]是A,items [1]是B,... items [25]是Z,现在我选择了项目[1],它是B,当我滚动collectionView,将取消选择项目[1],并将选择另一个项目。我知道问题是由重用引起的,但即使我使用google或StackOverflow中显示的某些解决方案,我也不知道如何正确修复它。
我的viewDidLoad:
collectionView.allowsMultipleSelection = false
我的CollectionViewCell类:
import UIKit
class AddCollectionViewCell: UICollectionViewCell {
}
我的CollectionView设置:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return iconSet.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "iconCell", for: indexPath) as! IconCollectionViewCell
cell.iconImage.image = UIImage(named:iconSet[indexPath.row])
return cell
}
我的CollectionView的didSelecteItemAt函数:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)
cell.backgroundView?.layer.cornerRadius = 5
}
我的CollectionView的didDeSelecteItemAt函数:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.0)
cell.backgroundView?.layer.cornerRadius = 5
}
}
答案 0 :(得分:0)
你可以尝试
var selectedIndex = 0 // set default or nil and check it's nullability before usage
//
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "iconCell", for: indexPath) as! IconCollectionViewCell
cell.iconImage.image = UIImage(named:iconSet[indexPath.row])
if indexPath.row == selectedIndex {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)
}
else {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.0)
}
return cell
}
我的CollectionView的didSelecteItemAt功能:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.row
collectionview.reloadData()
}
我的CollectionView的确是DeSelecteItemAt功能:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
// remove this
}
答案 1 :(得分:0)
您可以通过将所有选定项目的索引存储在单独的数组中来实现此目的,或者您可以在字典或自定义类别对象的数组中管理单独的属性,如library(ggplot2)
library(shiny)
mtcars$cyl = sample(letters[1:5], 32, TRUE)
ui <- fluidPage(
navbarPage(title="title",
tabPanel("One",
column(3,
wellPanel( selectInput('name', 'NAME', c("A", "B")))),
column(9, plotOutput('plot1')))
))
server <- function(input, output) {
X = reactive({input$name == "A"})
output$plot1 <- renderPlot({
if(X()){
p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( . ~ gear )
}else{
p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( cyl ~ gear )
}
return(p1)})
}
shinyApp(ui, server)
,无论您以何处填写你的collectionview单元格。
你可以简单地使用如下的数组: var arrIndex:[Int] = Int
isSelected