这是我的视图控制器,我在其中设置了我的 collectionView 及其方法。
class HomeScreenViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionViewOne: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionViewOne.register(UINib(nibName: "recomendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
return cell
}
这是我的自定义单元格文件
class CustomCellOneCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
我还使用下面的代码进行单元格声明。
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell
我检查了所有连接,仔细检查了标识符的名称。我的视图控制器是标签栏控制器的一部分。
