gpt4 book ai didi

swift - 第一次加载时 UICollectionView 中的 SelectItem

转载 作者:行者123 更新时间:2023-11-30 13:04:07 27 4
gpt4 key购买 nike

我有一个 UICollectionView,用户可以选择单元格来使用其中的函数。所以我需要它来将第一个单元格设置为选中,就像第一个屏幕截图中一样。但是存在的问题是,当选择单元格并且用户想要选择其他单元格时,第一个单元格不会取消选择。我尝试在 did select item 中重新加载 tableview,但不运行,或者在 viewdidload LabelCollectionView.selectItem(at: IndexPath(row: 1,section: 0),animated: true,scrollPosition: 。底部)

有人知道我可以在那里做什么吗?感谢您的帮助:)

First Cell Selected

Here you see the Problem in the screenshot

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryLabelCell", for: indexPath) as! StoryLabelCell


let labels = Labels[indexPath.row]


cell.backgroundColor = UIColor.lightGray
cell.layer.cornerRadius = 10

cell.Title.text = labels



if indexPath.row == 0 {
cell.backgroundColor = darkgrau
cell.layer.borderColor = black.cgColor
cell.layer.borderWidth = 2.0
}


return cell

}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell


if indexPath.row == indexPath.row {
cell.backgroundColor = darkgrau
cell.layer.borderColor = black.cgColor
cell.layer.borderWidth = 2.0
}

}




func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell


if indexPath.row == indexPath.row {

cell.backgroundColor = lightGray
cell.layer.borderWidth = 0
}

}

最佳答案

您想通过检查 indexPath.row == indexPath.row 来做什么?基本上,您可以在选择时尝试使用 cell.selected = true ,并在取消选择时将其设置为 false 。尝试下面的代码,让我知道它是否有效。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryLabelCell", for: indexPath) as! StoryLabelCell

let labels = Labels[indexPath.row]
cell.backgroundColor = UIColor.lightGray
cell.layer.cornerRadius = 10
cell.Title.text = labels

if indexPath.row == 0 {
cell.backgroundColor = darkgrau
cell.layer.borderColor = black.cgColor
cell.layer.borderWidth = 2.0
cell.selected = true
}

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell

cell.backgroundColor = darkgrau
cell.layer.borderColor = black.cgColor
cell.layer.borderWidth = 2.0
cell.selected = true
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! StoryLabelCell

cell.backgroundColor = lightGray
cell.layer.borderWidth = 0
cell.selected = false
}

关于swift - 第一次加载时 UICollectionView 中的 SelectItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39579659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com