【问题标题】:Update CollectionView cells that are not selected?更新未选中的 CollectionView 单元格?
【发布时间】:2016-02-28 17:41:03
【问题描述】:

我有一个 CollectionView,它允许用户触摸一个单元格,它会改变边框颜色。但是,我只想一次选择一个单元格。如何编辑此代码,以便使用边框颜色更新 indexpath 处的单元格并重置先前选择的单元格?

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        self.user["avatar"] = self.avatars[indexPath.row]

        do {
           try self.user.save()
        } catch {
            print(error)
        }

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.purpleColor().CGColor

谢谢!

更新

       let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
        var previouslySelectedIndexPath: NSIndexPath?
        if previouslySelectedIndexPath != nil {
            let previousCell = collectionView.cellForItemAtIndexPath(previouslySelectedIndexPath!) as! AvatarViewCell
            previousCell.layer.borderWidth = 0
            previousCell.layer.borderColor = UIColor.whiteColor().CGColor
        }

        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.purpleColor().CGColor

【问题讨论】:

    标签: ios swift uicollectionview swift2


    【解决方案1】:

    为什么你没有一个实例变量(在你的类文件的开头添加)来存储之前选择的单元格

    var previouslySelectedIndexPath: NSIndexPath?
    

    那么每次选中一个新单元格时,你先把之前选中的单元格去掉边框,再给新选中的单元格加上边框

    if previouslySelectedIndexPath != nil {
        let previousCell = collectionView.cellForItemAtIndexPath(previouslySelectedIndexPath!) as! AvatarViewCell
        previousCell.borderWidth = 0
    }
    let currentCell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
    cell.layer.borderWidth = 5.0
    cell.layer.borderColor = UIColor.purpleColor().CGColor
    previouslySelectedIndexPath = indexPath
    

    【讨论】:

    • 感谢您的回复!我更新了代码,但它似乎不起作用。我更新了我的问题。我需要在其他地方设置previouslySelectedIndexPath 吗?
    • 我注意到您在代码中将previouslySelectedIndexPath 声明放在didSelectItemAtIndexPath 中,这是错误的,它应该在您的类的顶部声明。
    【解决方案2】:

    你可以实现

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
    

    这是一个使用普通 UICollectionViewCell 的示例:

    // MARK: UICollectionViewDelegate
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
        if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
            cell.layer.borderWidth = 5.0
            cell.layer.borderColor = UIColor.purpleColor().CGColor
        }
    }
    
    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    
        if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
            cell.layer.borderWidth = 0
            cell.layer.borderColor = UIColor.whiteColor().CGColor
        }
    }
    

    【讨论】:

    • 我将单元格设置为什么?它必须不同于collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell 对吧?
    • 我在上面编辑了我的答案以包含示例代码。在你的情况下,是的,它会是collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
    • 这是最好的答案,因为它优雅地使用了框架而不添加不必要的状态跟踪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多