anusha hrithi anusha hrithi - 3 months ago 16
Swift Question

How to move to the next object using the array of count in swift?

I am working with infinite scrolling using uicollectionview.But I am trying when user click on the “Next”(Button) the should change to the next object. Like

I have an array like

" arrayOfItems = ["Apple","Ball","Cat","Rat”]”


If in collection view Cell I am displaying “Apple” in a label.

If I click on the next button “Ball” is coming.

If I click again on next button,It is not scrolling to next object.

My code is like this :

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

}

}


Can Anyone help me to solve this issue.What mistake I am doing here.

Thanks In Advance.

Answer

Your next button is always scrolling to row 1.
Try this :

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)



}