gpt4 book ai didi

swift - 如何使用swift中的计数数组移动到下一个对象?

转载 作者:行者123 更新时间:2023-11-28 16:16:05 28 4
gpt4 key购买 nike

我正在使用 uicollectionview 进行无限滚动。但是我正在尝试当用户单击“下一步”(按钮)时应该更改为下一个对象。喜欢

我有一个像 "arrayOfItems = ["Apple","Ball","Cat","Rat"]” 这样的数组

如果在 Collection View Cell 中,我在标签中显示“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

}

}

谁能帮我解决这个问题。我在这里犯了什么错误。

提前致谢。

最佳答案

你的下一个按钮总是滚动到第 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)



}

关于swift - 如何使用swift中的计数数组移动到下一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39076331/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com