我试图在一个 UIViewController 中设置 3 个 UICollectionView 如您所见,我创建了 3 个不同的 socket :
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 协议(protocol),对于 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 中:
Missing return in a function expected to return 'UICollectionViewCell'
最佳答案
您需要一个必须返回 UICollectionViewCell() 的 else 语句。
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
}
else{
return UICollectionViewCell() // any cell you must need to retrun. You can return nil if you want.
}
}
希望对您有所帮助。
关于ios - 在一个 UIViewController 中设置 3 个 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39328438/