视图出现时在collectionView中选择Item

时间:2018-09-19 12:51:11

标签: ios swift swift4.2

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

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
            }
        }
    }

2 个答案:

答案 0 :(得分: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内部进行切换

答案 1 :(得分: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
}

我希望它对您有用。