[英]Question about the problem of creating multiple collectionView in one viewController in Swift
我在一个视图控制器中创建了两个集合视图。 但我有一个简单的问题。
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'
你应该从问题发生的地方返回什么?
如果你只拥有 2 个 collectionViews,你可以这样做
if collectionView == self.collectionView {
...
} else {
...
}
或者将其更改为 switch 语句:
switch collectionView {
case self.collectionView:
// do something collectionView
default:
// do something for aLotViewCollectionView
}
您可以返回 UICollectionViewCell(),或者您可以放置一个 fatalError() 以确保在 function 被另一个集合视图调用的情况下捕获错误。
如果只有两个,则不需要“else 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.