Ios Swift 3 warning func collectionView实例方法几乎符合可选要求
我正在将我的项目转换为Xcode 8.1中的Swift 3,我的一个集合视图函数返回一条警告消息,指示封装外形已更改。代码是Ios Swift 3 warning func collectionView实例方法几乎符合可选要求,ios,uicollectionview,swift3,Ios,Uicollectionview,Swift3,我正在将我的项目转换为Xcode 8.1中的Swift 3,我的一个集合视图函数返回一条警告消息,指示封装外形已更改。代码是 func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell { 警告是 实例方法CollectionView(:CellForItemAtIndexPath:)几乎与协议UI
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {
警告是
实例方法CollectionView(:CellForItemAtIndexPath:)几乎与协议UICollectionViewDelegate的可选需求CollectionView(:canFocusItemAt:)匹配
我认为这个警告是在转移视线。我查阅了API参考,我发现
func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?
然而,我无法找到一个沿着这些行的声明,它不会导致带有红点的编译器错误
编辑:
在回答以下问题后,我将数据源添加到我的ViewController类中,如下所示:
class MyController: UIViewController,
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource,
然后在ViewDidLoad()
中,我添加了这个myCollectionView.dataSource=self
我现在有这个
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
此视图控制器完全是在代码中构造的,并且没有实现数据源,尽管在添加数据源后,numberOfSectionsInCollectionView
的代码生成了一个编译器红点。此更新为
func numberOfSections(in collectionView: UICollectionView) -> Int {
我认为这个警告是在转移视线
嗯,不是。通常,对待编译器的方式是尊重;它通常比你知道的多得多。你的建议是转移注意力
这是您想要的功能:
如您所见,签名是:
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
。。。这和你最初写的有微妙的不同
另一件需要注意的事情是,必须在显式采用UICollectionViewDataSource的类声明中进行此声明。唯一的例外是如果这是UICollectionViewController,在这种情况下它已经采用UICollectionViewDataSource-在这种情况下,编译器将告诉您向声明中添加
覆盖
(您应该遵守)。试着将它们设置为公共
。