【问题标题】:numberOfItemsInSection for multiple UICollectionView inside UIViewController SwiftnumberOfItemsInSection 用于 UIViewController Swift 内的多个 UICollectionView
【发布时间】:2018-11-09 04:25:25
【问题描述】:

我在一个UIViewController 中有三个UICollectionView。然后我为每个UICollectionView设置IBOutlet

@IBOutlet weak var firstCollectionView: UICollectionView!
@IBOutlet weak var secondCollectionView: UICollectionView!
@IBOutlet weak var thirdCollectionView: UICollectionView!

我为他们设置了委托和数据源。但是当我想像下面这样设置numberOfItemsInSection

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if collectionView == firstCollectionView {
            return 1
        } else if collectionView == secondCollectionView {
            return 2
        } else if collectionView == thirdCollectionView {
            return 3
        }
    }

它一直给我这个错误Missing return in a function expected to return 'Int'

【问题讨论】:

  • 在函数末尾添加 return 0 或将最后一个 else if 更改为 else。您收到此错误是因为此函数不知道有 3 个 collectionView。所以如果没有一个if 语句匹配,那么就没有返回
  • 为了更好的可读性,我认为你最好在这里使用 switch 语句。您可以将缺少的 return 0 放入 default 案例中。
  • 它解决了@Scriptable的问题
  • 它显示因为你没有返回 -> Int 为所需的功能,从 else if 中删除第三个集合视图条件并使其只有 else,错误将得到解决

标签: ios swift uicollectionview


【解决方案1】:

您需要返回 0,因为在 numberOfItemsInSection 中您必须返回 Int。你以 else if 结束,但 numberOfItemsInSection 仍然需要返回 Int 值。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == firstCollectionView {
        return 1
    } else if collectionView == secondCollectionView {
        return 2
    } else if collectionView == thirdCollectionView {
        return 3
    } else {
        return 0
    }
}

为了更好的可读性和更少的代码,我建议你使用switch 声明:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    switch collectionView {
    case firstCollectionView:
        return 1
    case secondCollectionView:
        return 2
    case thirdCollectionView:
        return 3
    default:
        return 0
    }
}

【讨论】:

  • @LinusGeffarth 谢谢你让我的回答更好:)
【解决方案2】:

就个人而言,我会为每个 collectionView 创建一个单独的 dataSource 类,并在 viewDidLoad 中分配它们 -

firstCollectionView.dataSource = FirstDataSource()
secondCollectionView.dataSource = SecondDataSource()
thirdCollectionView.dataSource = ThirdDataSource()

& 要么在创建每个数据源时为其提供所需的数据,要么让数据源也负责获取数据。在这种情况下将所有内容都放入 viewController 会导致疯狂...

简单的例子-

class MyDataSource: UICollectionViewDataSource {

    var data: [MyData] = [] // set this when instantiating, or provide the means of creating the data which you would have put in the viewController

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell: MyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) as? MyCollectionViewCell else {
            return UICollectionViewCell()
        }
        // do stuff to cell
    }
}

在视图控制器中-

self.firstCollectionView.dataSource = MyDataSource()
// etc...

非常直截了当,它所做的只是将代码分成责任区域,使其更易于管理

【讨论】:

  • 如何实现数据源?
  • 你能具体解释一下单独的数据源吗?
  • 不错的方法! @SomaMan
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
相关资源
最近更新 更多