所以我有2个控制器。登录页面名为viewController
,仪表板名为SecondViewController
。现在,您单击登录页面并转到第二个视图控制器,在其中创建CollectionView
。
self.performSegue(withIdentifier: "loginSuccessful", sender: self)
我想去SecondViewController
。现在,对于SecondViewController
,我创建了一个UICollectionView
,并在storyboard
上创建了自定义单元格,并将CollectionView
连接到数据源。现在,当我的登录信息转到我的SecondViewController
时,它会向我显示以下错误。
SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector
这是我的SecondViewController代码:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var UiToolbar: UINavigationItem!
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath)
return cell
}
}
PhotoCell
是我collectionViewCells
的标识符。我感谢任何能帮助我的人。
答案 0 :(得分:0)
我认为你做了一个简单的错误只是将扩展更改为Second View Controller。喜欢
extension SecondViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath)
return cell
}
}