在UICollectionView(SWIFT)中设置默认单元格?

时间:2017-07-05 03:10:17

标签: ios swift uicollectionview uicollectionviewcell

我是一名Swift初学者,试图在应用首次启动时实现一种在CollectionView中设置默认单元格的方法。我已经覆盖了我的'isSelected'方法,以图形方式在屏幕上显示所选单元格,例如; borderColor等...

到目前为止,我的方法(正确与否)是在'viewDidAppear'方法中调用'didSelectItemAt'方法。

我理解'didSelectItemAt'方法需要一个CollectionView作为它的第一个参数,但是当我在'viewDidAppear'中调用方法时,我把它作为第一个参数放在一起似乎无效。

任何想法?

extension DrinkMenuViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

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

        cell.imgDrinkType.image = imageArray[indexPath.row]
        cell.lbDrinkType.text = drinkTypeArray[indexPath.row]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTypeImageCollectionViewCell", for: indexPath) as! DrinkTypeImageCollectionViewCell

        cell.isSelected = true

        print("selected cell: ", indexPath)
    }

    override func viewDidAppear(_ animated: Bool) {
        let indexPathForFirstRow = IndexPath(row: 0, section: 0)

        collectionView(<#T##collectionView: UICollectionView##UICollectionView#>, didSelectItemAt: indexPathForFirstRow)

        print (indexPathForFirstRow)
    }
}

1 个答案:

答案 0 :(得分:0)

您不是显式调用任何API的委托方法的人。

func collectionView(_:didSelectItemAt:)UICollectionView的委托方法。 api本身处理何时调用此方法。因此,您不得在代码中的任何位置调用此方法。

再次,func collectionView(_:didSelectItemAt:)的实施没有任何意义。当您点击任何一个单元格时,将自动调用此方法。你不必在这里再次deque你的手机。