我如何符合UICollectionViewDelegate?

时间:2014-10-01 05:46:33

标签: swift uicollectionview protocols

得到协议不一致的编译错误。我错过了什么?

extension ViewController: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView!, section: Int) -> Int {
        //...
        return 5
    }

    func collectionView(collectionView: UICollectionView!, indexPath: NSIndexPath!) -> UICollectionViewCell! {
        let cell: AnyObject = collectionView.dequeueReusableCellWithReuseIdentifier("turkey", forIndexPath:indexPath)
        return cell as UICollectionViewCell
        //...
    }
}

swift:45:1: Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'

2 个答案:

答案 0 :(得分:0)

尝试删除所有!,因为代理人未根据文档使用选项。

答案 1 :(得分:0)

我刚刚对所有非可选API进行了剪切/过去并重新编译。
显然是有效的。

extension ViewController: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: AnyObject = collectionView.dequeueReusableCellWithReuseIdentifier("turkey", forIndexPath:indexPath)
        return cell as UICollectionViewCell

    }
}