我在一个viewController中创建了两个collectionview。但是我有一个简单的问题。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionView {
let mainCell = collectionView
.dequeueReusableCell(withReuseIdentifier: reuse.identifier.mainBookCell.rawValue, for: indexPath) as! MainBookCell
let url = URL(string: booksGenre?.books[indexPath.row].bookImage ?? "")
mainCell.bookImageView.kf.setImage(with: url)
return mainCell
} else if collectionView == self.aLotViewCollectionView {
let aLotViewCell = collectionView
.dequeueReusableCell(withReuseIdentifier: reuse.identifier.mainBookCell.rawValue, for: indexPath) as! ALotViewCell
let url = URL(string: booksGenre?.books[indexPath.row].bookImage ?? "")
aLotViewCell.bookImageView.kf.setImage(with: url)
return aLotViewCell
}
//problem!! return ?????
}
Missing return in a function expected to return 'UICollectionViewCell'
从发生问题的地方应该返回什么?
答案 0 :(得分:2)
如果仅要使用2个collectionView,可以执行
if collectionView == self.collectionView {
...
} else {
...
}
或将其更改为switch语句:
switch collectionView {
case self.collectionView:
// do something collectionView
default:
// do something for aLotViewCollectionView
}
答案 1 :(得分:0)
您可以返回UICollectionViewCell(),或者可以放置fatalError()以确保在使用另一个集合视图调用该函数的情况下捕获错误。
答案 2 :(得分:0)
如果只有两个,则不需要“ else if”。删除“ if”,以便仅此。
发件人:
else if collectionView == self.aLotViewCollectionView {
let aLotViewCell = collectionView
.dequeueReusableCell(withReuseIdentifier: reuse.identifier.mainBookCell.rawValue, for: indexPath) as! ALotViewCell
let url = URL(string: booksGenre?.books[indexPath.row].bookImage ?? "")
aLotViewCell.bookImageView.kf.setImage(with: url)
return aLotViewCell
}
收件人:
else {
let aLotViewCell = collectionView
.dequeueReusableCell(withReuseIdentifier: reuse.identifier.mainBookCell.rawValue, for: indexPath) as! ALotViewCell
let url = URL(string: booksGenre?.books[indexPath.row].bookImage ?? "")
aLotViewCell.bookImageView.kf.setImage(with: url)
return aLotViewCell
}