2016-08-18 11 views
0

をスクロール、私はのは私だけスクロールさせる必要がある3つのセルごとに3秒UICollectionView垂直に自動的に3秒ごとに

を追加し、自動的にコレクションビューのスクロールを行う必要があるアプリ

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() 
     } 
    } 

} 

を持っています私は三つのセル

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

が、アプリケーションのクラッシュやエラーを追加次の行があった:

01キャッチされない例外により「NSInvalidArgumentException」、理由にアプリを終了

:「無効なインデックスパスにスクロールしようとする試み:{長さ= 1、パス= 6}」

誰もがあなたがこれをやっている

+0

doAfterThreeSecodsそれはすべての3秒後に自己だ呼んでいます。 collectionView.reloadData()がここで呼び出されると、3秒ごとに呼び出されるようになります。 – Sofeda

+0

doAfterThreeSecondでscrollIndexがインクリメントされていない場合は、セルが追加される前にスクロールして –

+0

が呼び出されているか、またはセル番号が正しく計算されていません。 – Tj3n

答えて

0

を助けることができます間違った方法で。それは無効なインデックスパスエラーをスローする理由です。たとえば、3つのコレクションビューを表示することができます。 image01、image02、image03という名前にしましょう。したがって、配列は[image03、image01、image02、image03、image01]のようになります。最後の要素を最初の要素と最後の要素に追加する必要があります。答えは説明するのがかなり広いです。したがって、ここでは、このtheroyを記述する記事です。

Article :- 

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

関連する問題