【问题标题】:UICollectionView vertical scrolling automatically every 3 secondsUICollectionView 每 3 秒自动垂直滚动一次
【发布时间】:2016-12-25 06:59:40
【问题描述】:

我有一个应用程序需要在集合视图中自动滚动

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}”

任何人都可以提供帮助

【问题讨论】:

  • doAfterThreeSecods 在每 3sec 之后调用它自己。并且因为 collectionView.reloadData() 在这里被调用,所以它也是每 3 秒后调用一次。
  • scrollIndex 在 doAfterThreeSecond 调用中未增加
  • 在单元格追加或单元格数计算错误之前滚动,6个单元格为0->5

标签: ios swift


【解决方案1】:

您以错误的方式执行此操作。这就是它引发无效索引路径错误的原因。例如,假设您有 3 个集合视图要显示。我们将其命名为 image01,image02,image03。所以你的数组应该像 [image03,image01,image02,image03,image01]。您应该将最后一个元素附加到第一个元素,将第一个元素附加到最后一个。答案是相当广泛的描述。因此,这里有一篇描述这个理论的文章。

 Article :- 

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

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 2017-12-01
    • 2015-05-31
    • 2010-11-28
    • 2017-06-17
    • 2015-10-04
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多