【发布时间】: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