-3

我正在尝试在集合视图中选择我的第一个单元格时,这里出现的是它的代码

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let indexPath = IndexPath(item: 0, section: 0)
        categoryCollectionView.selectItem(at: indexPath
            , animated: false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))
        self.collectionView(categoryCollectionView, didSelectItemAt: indexPath)
    }

我在我的 collectionViewCell 类中有一个对 isSelected 变量的覆盖以更改文本颜色

override var isSelected: Bool {
        didSet {
            if self.isSelected {
                self.categoryName.textColor = UIColor(red: 237/255, green: 28/255, blue: 36/255, alpha: 1.0)
            } else {
                self.categoryName.textColor = UIColor(red: 63/255, green: 62/255, blue: 62/255, alpha: 1.0)
            }
        }
    }

但它不起作用

我通过操作 isSelected 属性来解决它,它工作正常

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.categoryCollectionView {
            if let cell = categoryCollectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as? CategoryCollectionViewCell {
                cell.configureCell(text: categories[indexPath.row].name)
                if indexPath.row == 0 {
                    cell.isSelected = true
                }
                return cell
            }
        } 
        return UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == self.categoryCollectionView {
            if indexPath != IndexPath(row: 0, section: 0) {
                collectionView.cellForItem(at: IndexPath(row: 0, section: 0))?.isSelected = false
            }
        }
    }
4

2 回答 2

1
            override func viewDidAppear(_ animated: Bool) {
              super.viewDidAppear(animated)

              let indexPathForFirstRow = IndexPath(row: 0, section: 0)

              categoryCollectionView.selectItem(at: indexPathForFirstRow, animated:false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))

              self.collectionView(paymentHeaderCollectionView, didSelectItemAt: indexPathForFirstRow)     
    }

当用户选择另一个单元格时,在 DidSelect 内部,您可以像这样取消选择单元格

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
    let firstCell = IndexPath(item: 0, section: 0)
    if indexPath != firstCell {
        collectionView.deselectItem(at: indexPath, animated: false)
       }
}

对于 isSelect 部分,你可以试试这个

override var selected: Bool {
    get {
        return super.selected
    }
    set {
        if newValue {
            super.selected = true
             //your Color
            println("selected")
        } else if newValue == false {
            super.selected = false
          //your Color
            println("deselected")
        }
    }
}

尽管如此,这可能会导致问题,在我看来,更简单和最好的解决方案是在您填充的对象内创建一个标志 isSelected 并检查它们并在 didSelect 内切换它们

于 2018-09-19T13:07:35.927 回答
0

你可以像下面这样尝试,如果单元格在 0 索引中,它将对我有用。

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

if (indexPath.row == 0){
    collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
    cell.layer.borderColor=UIColor.gray.cgColor        
}else{
    cell.layer.borderColor=UIColor.white.cgColor
}
    return cell
}

我希望它对你有用。

于 2018-09-20T05:41:06.343 回答