我为我的应用程序提供了此自定义滚动视图,并且我的主委托类中有一个数据数组。我也有一个单独的扩展名(在同一文件中)来处理数据,但似乎无法访问带有两个错误的数据Use of unresolved identifier data
:
class HomeViewController: UIViewController {
fileprivate let data = [
CustomData(title: "Test", image: #imageLiteral(resourceName: "splash_icon"), url: "clipifyapp.com"),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: "clipifyapp.com"),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: "clipifyapp.com")
]
}
extension UIViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count // ERROR ONE
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.data = self.data[indexPath.row] // ERROR TWO
return cell
}
}
我缺少明显的东西吗?
您正在扩展UIViewController
,但您不是要扩展HomeViewController
吗?
如果扩展UIViewController
,则将扩展整个应用程序中的每个UIViewController,这可能不是您要使用UICollectionViewDataSource做的。
[data
仅在HomeViewController
中可用,在基本类UIViewController
中不可用>
替换
extension UIViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
with
extension HomeViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {