gpt4 book ai didi

ios - UICollectionView 每 3 秒自动垂直滚动一次

转载 作者:行者123 更新时间:2023-11-30 13:08:36 25 4
gpt4 key购买 nike

我有一个应用程序需要在 Collection View 中自动滚动

class resultsViewController: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
var numberCount = 3
var scrollIndex = 1
override func viewDidLoad() {
super.viewDidLoad()

let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
let width = UIScreen.mainScreen().bounds.size.width - 6
flow.itemSize = CGSizeMake(width/3, width/3)
flow.minimumInteritemSpacing = 3
flow.minimumLineSpacing = 3
flow.sectionInset = UIEdgeInsetsMake(0.0, 0.0,10,0);





// Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.doAfterThreeSecods()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageSearch", forIndexPath: indexPath) as! imagesCollectionViewCell
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberCount
}

func doAfterThreeSecods()
{

numberCount += 3
collectionView.reloadData()

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {

self.doAfterThreeSecods()
}
}

}

每三秒追加 3 个单元格

当我追加三个单元格时,我只需要滚动下一行

  self.collectionView.layoutIfNeeded()
self.collectionView.scrollToItemAtIndexPath(NSIndexPath(index: self.scrollIndex), atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: true)

但是应用程序崩溃并且错误是:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“尝试滚动到无效索引路径:{length = 1,path = 6}”

任何人都可以提供帮助

最佳答案

您这样做的方式是错误的。这就是它抛出无效索引路径错误的原因。例如,假设您有 3 个 Collection View 要显示。我们将其命名为 image01、image02、image03。所以你的数组应该像[image03,image01,image02,image03,image01]。您应该将最后一个元素附加到第一个元素,将第一个元素附加到最后一个元素。答案非常广泛,难以描述。因此这里有一篇描述这个理论的文章。

 Article :- 

https://adoptioncurve.net/archives/2013/07/building-a-circular-gallery-with-a-uicollectionview/

关于ios - UICollectionView 每 3 秒自动垂直滚动一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39016622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com