【发布时间】:2016-09-05 10:17:30
【问题描述】:
我正在尝试在一个 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”的函数中缺少返回
【问题讨论】:
-
如果没有任何条件匹配,您的“cellForItemAt”函数不会返回任何单元格。您可以简单地在“return UICollectionViewCell()”等条件结束时返回空单元格
标签: ios swift uicollectionview