我正在使用Xcode 8.1将我的项目转换为Swift 3,我的一个集合视图函数返回了一个警告信息,指示占用空间已经改变。代码如下:
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {
警告内容为:
实例方法CollectionView(:CellForItemAtIndexPath:)几乎匹配UICollectionViewDelegate协议的可选要求collectionView(:canFocusItemAt:)
我认为这个警告在这里是一个误导信息。我查看了API参考https://developer.apple.com/reference/uikit/uicollectionview/1618088-cellforitem,并发现
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 {