首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何符合UICollectionViewDelegate?

如何符合UICollectionViewDelegate?
EN

Stack Overflow用户
提问于 2014-10-01 13:46:33
回答 2查看 1.5K关注 0票数 0

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

代码语言:javascript
复制
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
        //...
    }
}

代码语言:javascript
复制
swift:45:1: Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'
EN

回答 2

Stack Overflow用户

发布于 2014-10-01 13:51:25

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

票数 0
EN

Stack Overflow用户

发布于 2014-10-02 02:19:12

我只是对所有非可选的API进行了剪切/粘贴并重新编译。

显然,这是可行的。

代码语言:javascript
复制
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

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26134850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档