ios - 在一个 UIViewController 中设置 3 个 UICollectionView

标签 ios swift uicollectionview

我试图在一个 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'

enter image description here

最佳答案

您需要一个必须返回 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/

相关文章:

ios - UILabel 和 NSLayoutConstraint 的垂直列表

ios - swift 错误 : Struct 'XX' must be completely initialized before a member is stored to

ios - 在 Collection View Controller 中快速创建两个 View

ios - 在 Swift 中以编程方式更改 Storyboard 的类

ios - 由于 dequeueWithReusableIdentifier,UICollectionView 加载速度非常慢,我可以在后台加载它吗?

swift - 如何获取 UICollectionView 中每个选定单元格的按钮标题?

iphone - 'NSString' 没有可见的@interface 声明选择器 'appendData'

objective-c - iOS 通过应用内购买从服务器下载非消耗性内容

swift - 午睡处理多个请求

ios - 使用 Swift,在 iOS 中标题位于顶部和右侧的 Collection View