无法在标签中显示细胞总数中的细胞计数(例如:2/5)用于Swift中的collectionview

js5cn81o  于 2023-04-04  发布在  Swift
关注(0)|答案(1)|浏览(117)

我正在使用leftButtonrightButton滚动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
}
goqiplq2

goqiplq21#

有几点需要修正:

  • 需要将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) {
guard let visibleIndexPath = collectionView.indexPathsForVisibleItems.first else { return }
currentItem = visibleIndexPath.row //updated
//currentItem = indexPath.row //removed line
}

相关问题