我正在尝试在一个UIViewController中设置3个UICollectionView,我创建了3个不同的插座,如下所示:
class myViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var myCollectionView1: UICollectionView!
@IBOutlet weak var myCollectionView2: UICollectionView!
@IBOutlet weak var myCollectionView3: UICollectionView!
并为他们每个人
override func viewDidLoad() {
super.viewDidLoad()
self.myCollectionView1.delegate = self
self.myCollectionView1.dataSource = self
self.myCollectionView2.delegate = self
self.myCollectionView2.dataSource = self
self.myCollectionView3.delegate = self
self.myCollectionView3.dataSource = self
}
对于UICollectionView DataSource协议,用于Section
func numberOfSections(in collectionView: UICollectionView) -> Int {
if (collectionView == self.myCollectionView1) {
return 0
} else if (collectionView == self.myCollectionView2) {
return 0
} else if (collectionView == self.myCollectionView3) {
return 0
}
return 0
}
对于numberOfItemsInSections
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (collectionView == self.myCollectionView1) {
return self.allName.count
} else if (collectionView == self.myCollectionView2) {
return self.pharmacyName.count
} else if (collectionView == self.myCollectionView3) {
return self.clinicName.count
}
return 0
}
以及cellAtIndexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == myCollectionView1) {
let cell1: allBrandCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell1", for: indexPath) as! allBrandCollectionViewCell
cell1.name.text = allName[indexPath.row]
cell1.phoneNumber.text = allPhoneNumber[indexPath.row]
return cell1
} else if (collectionView == myCollectionView2) {
let cell2: pharmacyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell2", for: indexPath) as! pharmacyCollectionViewCell
cell2.name.text = pharmacyName[indexPath.row]
cell2.phoneNumber.text = pharmacyPhoneNumber[indexPath.row]
return cell2
} else if (collectionView == myCollectionView3) {
let cell3: clinicCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell3", for: indexPath) as! clinicCollectionViewCell
cell3.name.text = clinicName[indexPath.row]
cell3.phoneNumber.text = clinicPhoneNumber[indexPath.row]
return cell3
}
}
所以现在当我尝试构建项目时他们会在cellForRowAtIndexPath中向我显示错误:
在预期返回'UICollectionViewCell'的函数中缺少返回
5 回答
"cellForItemAt"具有非可选的UICollectionViewCell返回类型 . 但是如果条件都不匹配,则代码不会返回单元格 .
如果没有条件匹配,则可以返回空的UICollectionViewCell对象 .
更正后的代码:
您需要一个必须返回UICollectionViewCell()的else语句 .
希望能帮助到你 .
你应该再添加一行像
return nil
或者返回tableview cell object
,因为如果你的每个if语句都没有执行,那么函数必须返回一些东西 .在
cellfor..
函数的if语句中的第二件事,你应该在每个if语句中使用self.myCollectionView1
不仅myCollectionView1
,所以要正确!!目前您的集合视图的
cellForItemAt indexPath
无法获取return
. 因为您已将return
放入if()
的范围内 . 要解决此问题,您需要在if()
的范围外再添加一个return
,它必须在所有if()
和其他条件之后 .如
以下是示例代码段:
你必须返回一个单元格 in all cases ,而你根本没有 grab 每种可能的情况 . [如果cellForItemAt方法中的单元格不是myCollectvion1-3中的任何一个怎么办?]
因此,您的解决方案可以通过两种方式完成:
1) 将此行放在cellForRow方法的末尾:
要么
2) make else {}