【问题标题】:Set 3 UICollectionView in one UIViewController在一个 UIViewController 中设置 3 个 UICollectionView
【发布时间】: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


【解决方案1】:

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

希望对你有帮助。

【讨论】:

    【解决方案2】:
    • “cellForItemAt”有一个非可选的 UICollectionViewCell 返回类型。 但是如果没有任何条件,您的代码不会返回单元格 火柴。
    • 如果没有,您可以返回一个空的 UICollectionViewCell 对象 条件数学。

    以下更正代码:

    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
    
        }
       return UICollectionViewCell()
    }
    

    【讨论】:

      【解决方案3】:

      您应该在最后一行添加一个像 return nil 或返回 tableview cell object 因为如果您的每个 if 语句没有执行,那么函数必须返回一些东西。

      cellfor.. 函数中的 if 语句中的第二件事,您应该在每个 if 语句中使用 self.myCollectionView1 而不仅仅是 myCollectionView1,所以要正确!

      【讨论】:

        【解决方案4】:

        目前您的收藏视图的cellForItemAt indexPath 无法获取return。因为你已经把return 放到了if() 的范围内。 要解决这个问题,你需要在if()的范围外再增加一个return,并且必须在所有if()等条件之后。

        作为

        return nil or return 0
        

        这里是示例 sn-p:

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
            if (collectionView == self.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 == self.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 == self.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
        
            }
        
        return nil
        }
        

        【讨论】:

          【解决方案5】:

          在所有情况下您都必须返回一个单元格,而您根本没有抓住每一种可能的情况。 [如果 cellForItemAt 方法中的单元格不是 myCollectvion1-3 中的任何一个怎么办?]

          因此您的解决方案可以通过两种方式完成:

          1) 将此行放在 cellForRow 方法的末尾:

          ... 
          
          return UITableViewCell() // like you have done in other method using `return 0`
          

          2) ma​​ke else {}

            ...       
          
              return self.clinicName.count
          
            } else {
                return UITableViewCell() // or any cell you want to create    
            }    
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-08-12
            • 1970-01-01
            • 2023-03-24
            • 1970-01-01
            • 1970-01-01
            • 2021-10-20
            • 1970-01-01
            相关资源
            最近更新 更多