Ios 首次加载时自动选择的项目

Ios 首次加载时自动选择的项目,ios,swift3,uicollectionview,selecteditem,didselectrowatindexpath,Ios,Swift3,Uicollectionview,Selecteditem,Didselectrowatindexpath,我正在使用水平collectinview显示项目。当用户选择/取消选择项目时,我将在文本下添加/删除白色边框 这是密码 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if let cell = collectionView.cellForItem(at: indexPath) { if cell.isSelected {

我正在使用水平collectinview显示项目。当用户选择/取消选择项目时,我将在文本下添加/删除白色边框

这是密码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath)  {

        if cell.isSelected {

            bottomLayer.frame = CGRect(x: 0, y: (cell.frame.height) - 7, width: (cell.frame.width), height: 3)
            bottomLayer.backgroundColor = UIColor.white.cgColor
            cell.layer.addSublayer(bottomLayer)
        }
    }
}


func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    if collectionView.cellForItem(at: indexPath) != nil {

            bottomLayer.backgroundColor = UIColor.green
    }
}
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
CollectionView.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: [])


collectionView(CollectionView, didSelectItemAt: indexPathForFirstRow)
我想要的是,当我加载集合视图时,第一个项目应该使用
bottomLayer
(带下划线)加载

我试过密码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath)  {

        if cell.isSelected {

            bottomLayer.frame = CGRect(x: 0, y: (cell.frame.height) - 7, width: (cell.frame.width), height: 3)
            bottomLayer.backgroundColor = UIColor.white.cgColor
            cell.layer.addSublayer(bottomLayer)
        }
    }
}


func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    if collectionView.cellForItem(at: indexPath) != nil {

            bottomLayer.backgroundColor = UIColor.green
    }
}
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
CollectionView.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: [])


collectionView(CollectionView, didSelectItemAt: indexPathForFirstRow)

但不起作用。我搜索了很多问题,但大部分都有相同的解决方案,在我的案例中它不起作用。这里有人能帮我吗?

如果您正在加载按钮操作的
CollectionView
,那么

CollectionView.layoutIfNeeded()
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
CollectionView.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: [])
collectionView(CollectionView, didSelectItemAt: indexPathForFirstRow)
视图中调用它

override func viewDidAppear(_ animated: Bool) {
    let indexPathForFirstRow = IndexPath(row: 0, section: 0)
    CollectionView.selectItem(at: indexPathForFirstRow, animated: true, scrollPosition: [])
    collectionView(CollectionView, didSelectItemAt: indexPathForFirstRow)
}

我正在加载按钮操作事件的collectionview。所以我在collectionview加载之后添加了这段代码。它触发了didSelectItem方法,但在“if let cell=collectionView.cellForItem(at:indexPath)”失败。有什么建议吗???
collectionView.layoutIfNeeded()
!!非常感谢你。