我正在使用
leftButton
rightButton
滚动 collectionview 单元格
最初需要出示5张门票中的1张
如果我点击 rightButton 然后点击 2 of 5 tickets,再次点击 rightButton 然后点击 3 of 5 tickets
现在如果我点击 leftButton 然后 5 张票中的 2 张像这样
我试过下面的代码:
currentItem
用于水平滚动的单元格,但是如果我将它用于票数它不起作用所以我又采用了一个变量 ticketCount
来显示总票数
但是这段代码也不起作用
如果我达到了 5 个单元格中的 1 个,如果我再次点击 leftButton,那么它也会变成 -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
}
需要修正的几点:
通过更新您的价值观来尝试此代码
var ticketCount = 1
var currentItem: Int = 0 // initialize currentItem to 0
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = "\(ticketCount) of \(ticketListtData?.result?.tickets?.count ?? 0) Tickets"
}
@IBAction func leftButton(_ sender: UIButton) {
if currentItem > 0 { // check if currentItem is already at the first item
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 { // check if currentItem is already at the last item
ticketCount += 1
titleLabel.text = "\(ticketCount) of \(totalItems) 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
}