【问题标题】:SelectItem in UICollectionView on first Load首次加载时 UICollectionView 中的 SelectItem
【发布时间】:2017-01-27 12:37:49
【问题描述】:

我有一个 UICollectionView,用户可以在单元格上选择以使用其中的函数。所以我需要它来设置第一个单元格,就像在第一个屏幕截图中一样。 But the Problem there is when the cell is selected and user want to select ah other cell the first cell don´t unselect.我已经尝试在确实选择的项目中重新加载表格视图,但它不会运行或在 viewdidload LabelCollectionView.selectItem(at: IndexPath(row: 1, section: 0), animated: true, scrollPosition: .bottom) 中使用这个函数@

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

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
    }

}

【问题讨论】:

    标签: swift select uicollectionview row cell


    【解决方案1】:

    您要检查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
    }
    

    【讨论】:

    • 我曾经在 {index path.row == index path.row}else {} 中将其他单元格设置为浅灰色(未选中),但它没有运行,他只给了我选中的细胞。所以它运行所有,但问题是当我想选择另一个单元格时,第一个单元格(选择的内容)不会消失,然后选择了两个单元格。无论如何,当我点击另一个单元格时,我必须更改第一个单元格的选择(浅灰色)。
    • 是的,这就是它的工作原理,您应该将其标记为 selected = true 以更改 UI 并在选择其他单元格时将其设置回 false。上面的代码你试过了吗?
    • 是的,我已经尝试过了,但它没有运行,也许在 didSelectItemAt 中给它另一种方式?
    • 您遇到什么错误?你得到什么输出?
    • 当我点击其他单元格索引时选择了那个到单元格。当我在第一个单元格上点击两次,然后在另一个单元格上运行时,它会运行,但这对用户不友好。因此,当我在 didselectItem 中点击另一个单元格时,我必须将背景设置为浅灰色(未选中)。
    【解决方案2】:

    你应该使用 Cells selected 状态而不是比较两个相同的值。

    在您的自定义单元格中,您会对单元格选择做出反应!

    override func setSelected(_ selected: Bool,  animated: Bool) {
        // change your cell like you want
        if selected {
            // make me dark
        } else { 
            // make me light
        }
        // not tested, but somehow call the super
        super.setSelected(selected:selected, animated: animated)
    }
    

    你可以删除你的 didSelect 和 didDeselct 函数。

    在函数 cellForItem ...中,您可以检查是否选择了一个单元格,如果没有选择具有 indexPath.row == 0 的单元格

    用于检查选定的单元格使用

    collectionView.indexPathForSelectedItems
    

    这是列表,因此您可以选择多个单元格。我不确定,但我认为您可以使用多项选择或仅一项选择来设置 collectionView。

    希望到目前为止对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多