【问题标题】:Int max crashing when trying to set numberOfItemsInSection for collection View circular scroll尝试为集合视图循环滚动设置 numberOfItemsInSection 时,Int max 崩溃
【发布时间】:2018-03-16 11:17:55
【问题描述】:

目前,我的收藏视图中有 3 个单元格,我希望像在 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> 1 之后那样在两个方向上旋转。因此用户可以双向滚动。为了实现这一点,我在 numberOfItemsInSection 中返回 Int.max

var max = Int.max
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return max
}

但应用程序崩溃,控制台中没有日志。 如果我给它一个真正的日志值,比如 10K,它就会起作用。谁能告诉我为什么会崩溃(使用 iPhone 6 运行代码)

print(max) //9223372036854775807 log 

cellForItemAt indexPath 不是问题,因为代码在 numberOfItemsInSection 处停止并且永远不会到达 cellForItemAt。下面仍然是 cellForItemAt indexPath 的代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let itemToShow = viewControllers[indexPath.row % viewControllers.count]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SwipeTabCollectionViewCell", for: indexPath) as! SwipeTabCollectionViewCell
    cell.title.text = itemToShow
    return cell
}

【问题讨论】:

  • 不要使用 max...只使用 100 之类的数字,并在滚动视图达到中间点左右时继续插入更多值
  • 这里有一个可以帮助你的 pod:cocoapods.org/pods/PMCircularCollectionView
  • @Tj3n 我喜欢你的方法并这样做 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { if indexPath.item == (max/2) { max =最大 * 2 tabBarCollectionView.reloadData() } } 。但是在 max 从 10 (10* 2) cellForItemAt 变为 20 之后,即使我滑动了 3 次,也停止调用

标签: ios swift uicollectionview swift4


【解决方案1】:

刚刚对此进行了测试并为我工作。将 max 更改为 let 或仅使用下面的代码。

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

【讨论】:

  • 起初,我只是这样做,但运气不好,它崩溃了,也许它的操作系统或架构相关的 Int 在不同的架构中被不同地对待
  • 看看来自stackoverflow.com/questions/48076430/…的第一个答案,它在scrollView上使用了一个很好的技巧
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多