【问题标题】:How do I conform to UICollectionViewDelegate?我如何符合 UICollectionViewDelegate?
【发布时间】:2014-10-01 05:46:32
【问题描述】:

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

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'

【问题讨论】:

    标签: swift uicollectionview protocols


    【解决方案1】:

    请尝试删除所有 !,因为根据文档,委托未使用可选项。

    【讨论】:

    • 哦,两个方法名都错了。中间少了一个部分。根据文档修复名称,它应该可以工作。
    【解决方案2】:

    我只是对所有非可选 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
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2011-03-13
      • 2017-02-27
      相关资源
      最近更新 更多