2016-08-22 33 views
0

我正在用無限滾動使用uicollectionview.But我正在嘗試當用戶單擊「下一步」(按鈕)應改變到下一個對象。像如何使用swift中的數組數組移動到下一個對象?

我有一個像" arrayOfItems = ["Apple","Ball","Cat","Rat」]」

數組如果收集觀察室我的標籤顯示「蘋果」。

如果我點擊下一個按鈕「球」即將到來。

如果我再次點擊下一個按鈕,它不會滾動到下一個對象。

我的代碼是這樣的:

func reversePhotoArray(arrayItems:[String], startIndex:Int, endIndex:Int){ 
     if startIndex >= endIndex{ 
      return 
     } 
     swap(&arrayOfItems[startIndex], &arrayOfItems[endIndex]) 

     reversePhotoArray(arrayOfItems, startIndex: startIndex + 1, endIndex: endIndex - 1) 
    } 

} 
    @IBAction func nextButtonMethod(sender: AnyObject) { 


     // let fullyScrolledContentOffset:CGFloat = infiniteScrollingCollectionView.frame.size.width * CGFloat(photosUrlArray.count - 1) 


     // reversePhotoArray(photosUrlArray, startIndex: 0 + 1, endIndex: photosUrlArray.count - 1) 


      reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: photosUrlArray.count - 1) 
      reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: 1) 

      reversePhotoArray(arrayOfItems, startIndex: 2, endIndex: arrayOfItems.count - 1) 
     reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: arrayOfItems.count - 1) 
     reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: arrayOfItems.count - 3) 
     reversePhotoArray(arrayOfItems, startIndex: arrayOfItems.count - 2, endIndex: arrayOfItems.count - 1) 
      let indexPath : NSIndexPath = NSIndexPath(forRow: 1, inSection: 0) 
      infiniteScrollingCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false) 



//   let indexPath : NSIndexPath = NSIndexPath(forRow: photosUrlArray.count - 2, inSection: 0) 
//   infiniteScrollingCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false) 



    } 


extension InfiniteScrollingViewController : UICollectionViewDataSource{ 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return 1 

    } 



    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return arrayOfItems.count 

    } 



    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! InfiniteScrollingCell 

     let photoName = photoForIndexPath(indexPath) 

     cell.label.text = photoName 

    cell.configureCell(photoName) 



     return cell 

    } 

} 

有人能幫助我解決這個issue.What錯誤,我在這裏做什麼。

在此先感謝。

回答

1

你的下一個按鈕總是滾動到第1行
試試這個:

var nextRow = 1 

@IBAction func nextButtonMethod(sender: AnyObject) { 

    // let fullyScrolledContentOffset:CGFloat = infiniteScrollingCollectionView.frame.size.width * CGFloat(photosUrlArray.count - 1) 

    // reversePhotoArray(photosUrlArray, startIndex: 0 + 1, endIndex: photosUrlArray.count - 1) 


    reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: photosUrlArray.count - 1) 
    reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: 1) 

    reversePhotoArray(arrayOfItems, startIndex: 2, endIndex: arrayOfItems.count - 1) 
    reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: arrayOfItems.count - 1) 
    reversePhotoArray(arrayOfItems, startIndex: 0, endIndex: arrayOfItems.count - 3) 
    reversePhotoArray(arrayOfItems, startIndex: arrayOfItems.count - 2, endIndex: arrayOfItems.count - 1) 

    if nextRow < arrayOfItems.count { 
     let indexPath : NSIndexPath = NSIndexPath(forRow: nextRow, inSection: 0) 
     infiniteScrollingCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false) 
     nextRow += 1 
    } 

//   let indexPath : NSIndexPath = NSIndexPath(forRow: photosUrlArray.count - 2, inSection: 0) 
//   infiniteScrollingCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false) 



} 
+0

導致崩潰。如果我的「老鼠倉」的數據錯誤後點擊:「試圖滾動到無效的索引路徑: {length = 2,path = 0 - 4}「 –

+0

@anushahrithi在」Rat「之後你想要做什麼?回到第一個還是什麼都不做? –

+0

謝謝。它正在工作。 –

相關問題