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

标签 swift select uicollectionview row cell

我有一个 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/

相关文章:

java - 在 JTextPane 中选择多个项目

php - MySql Select 查询返回错误结果

ios - 如何在 swift 中均匀移动两个 Collection View 单元格?

ios - UICollectionView - 图像是随机设置的

swift - 如何解决 Swift 中的 "Generic parameter ' T' 无法推断”

ios - swift UIImage 蓝色正方形

ios - Lister 示例项目 - Swift 版本的构建错误

swift - 如何在非事件 watch 收到本地通知时触发触觉警报

mysql - 选择 MYSQL 之后进行连接

ios - 嵌入 tableviewHeader 中的弹性 collectionView