【问题标题】:Select Item in collectionView when the view appears视图出现时选择collectionView中的Item
【发布时间】:2018-09-19 12:51:10
【问题描述】:

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

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

【问题讨论】:

标签: ios swift swift4.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 内切换它们

【讨论】:

  • 它应该可以工作,但我有一个错误原因 .left '尝试滚动到无效的索引路径: {length = 2, path = 0 - 0}'
  • 试试 .top 而不是 .left
  • 如果这对你有用,请注意我,@MohamedFarid 快乐编码
  • 同样的错误我几乎尝试了所有可用的选项,唯一有效的是UICollectionView.ScrollPosition(rawValue: 0)
  • 好的,我编辑了尝试新选择的覆盖,并添加一个断点
【解决方案2】:

你可以像下面这样尝试,如果单元格在 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
}

我希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多