如何使用swift中的count数组移动到下一个对象?
How to move to the next object using the array of count in swift?
我正在使用 uicollectionview.But 进行无限滚动 我正在尝试当用户单击“下一步”(按钮)时应该更改为下一个对象。喜欢
我有一个像 " arrayOfItems = ["Apple","Ball","Cat","Rat”]”
这样的数组
如果在集合视图单元格中,我在标签中显示“Apple”。
如果我点击下一个按钮,“球”就来了。
如果我再次点击下一个按钮,它不会滚动到下一个对象。
我的代码是这样的:
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 行。
试试这个:
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)
}
我正在使用 uicollectionview.But 进行无限滚动 我正在尝试当用户单击“下一步”(按钮)时应该更改为下一个对象。喜欢
我有一个像 " arrayOfItems = ["Apple","Ball","Cat","Rat”]”
如果在集合视图单元格中,我在标签中显示“Apple”。
如果我点击下一个按钮,“球”就来了。
如果我再次点击下一个按钮,它不会滚动到下一个对象。
我的代码是这样的:
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 行。
试试这个:
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)
}