我正在使用uicollectionview进行无限滚动。但我正在尝试当用户点击“下一步”(按钮)时应该更改为下一个对象。像
我有一个像" arrayOfItems = ["Apple","Ball","Cat","Rat”]”
如果在集合视图中,我在标签中显示“Apple”。
如果我点击下一个按钮“Ball”即将到来。
如果我再次点击下一个按钮,它就不会滚动到下一个对象。
我的代码是这样的:
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
}
}
任何人都可以帮我解决这个问题。我在这里犯了什么错误。
提前致谢。
答案 0 :(得分: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)
}