我正在使用leftButton
rightButton
滚动collectionview单元格
最初需要显示5张票中的1张
如果我点击右按钮,然后点击5张中的2张,再次点击右按钮,然后点击5张中的3张
现在,如果我点击左按钮,然后2的5票这样
我已经尝试了下面的代码:currentItem
用于水平滚动的单元格,但如果我使用这个票计数它不工作,所以我已经采取了一个变量ticketCount
显示ticketCount的总数
但是这段代码也不起作用
如果我达到了5个单元格中的1个,如果我再次点击左按钮,那么它也会变成-Ve值,总计数也超过了总计数,为什么?
我哪里错了,请您指点。
var ticketCount = 1
var currentItem: Int?
@IBAction func leftButton(_ sender: UIButton) {
if currentItem != 0, currentItem != nil {
ticketCount -= 1
titleLabel.text = "\((ticketCount)) of \(ticketListtData?.result?.tickets?.count ?? 0) Tickets"
currentItem! -= 1
let indexPath = IndexPath(item: currentItem!, section: 0)
qrCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
adCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
} else {
print("No item to go to previous left")
}
}
@IBAction func rightButton(_ sender: UIButton) {
guard let totalItems = ticketListtData?.result?.tickets?.count else { return }
if currentItem! < totalItems - 1 {
ticketCount += 1
titleLabel.text = "\((ticketCount)) of \(ticketListtData?.result?.tickets?.count ?? 0) Tickets"
currentItem! += 1
let indexPath = IndexPath(item: currentItem!, section: 0)
qrCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
adCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
} else {
print("No item to go to next right")
}
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
currentItem = indexPath.row - 1
}
1条答案
按热度按时间goqiplq21#
有几点需要修正:
通过更新您值来尝试此代码