无法在 swift 的集合视图中显示标签中总单元格中的单元格计数(例如:5 个中的 2 个)

问题描述 投票:0回答:1

我正在使用

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
}
swift button count uicollectionview
1个回答
0
投票

需要修正的几点:

  • 需要将currentItem的初始值设置为0。
  • 您正在递减 leftButton 函数中的 currentItem 变量,而没有检查它是否已经在第一项。
  • 在您的 rightButton 函数中添加一个条件,以检查 currentItem 是否已经在递增之前的最后一项。 您的 will Display 函数,您正在从 indexPath.row 值中减去 1 以设置当前项目。只需将 currentItem 设置为 indexPath.row.

通过更新您的价值观来尝试此代码

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
}
© www.soinside.com 2019 - 2024. All rights reserved.