我有一个UICollectionView,用户可以选择单元格来使用它的功能。所以我需要它将第一个单元格设置为在第一个屏幕截图中进行选择。但是问题出现在单元格被选中并且用户想要选择第一个单元格不能取消选择的其他单元格。我尝试它来重新加载tableview中没有选择的项目,但不能运行或与此func在viewdidload LabelCollectionView.selectItem(at: IndexPath(row: 1, section: 0), animated: true, scrollPosition: .bottom)
SelectItem在UICollectionView上第一次加载
有没有人有一个想法,我可以在那里做什么?感谢您的帮助:)
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
}
}
我曾经在{index path.row == index path.row} else {}中设置了浅灰色(未选中)的其他单元格,但它没有运行,只给出了我选中的单元格。所以它运行的所有,但问题是当我想选择其他细胞的第一个细胞(选择什么)不离开,然后选择两个细胞。无论如何,当我点击其他细胞时,我必须改变第一个细胞的选择(浅灰色)。 –
是的,它是如何工作的,你应该将它标记为selected = true来更改UI,并在选择其他单元格时将其设置为false。使用上述代码进行操作吗? – Santosh
是的,我已经尝试过,但它不能运行给它也许在didSelectItemAt其他方式? –