当我在 swift 中使用左右按钮一个一个地滚动时,Collectionview 单元格计数显示错误

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

我需要水平滚动 collectionview,如果我单击右键(每次)计数应该增加 +1 到总计数如果我单击左键然后计数将减少最多 1

如果计数为 0:标签为空

如果点数是 1: 1 of 1 Ticket

如果计数大于 1:则 1 of (ticketListtData?.result?.tickets?.count ?? 0) Tickets

如果总共 3 然后单击右键 >>>> 1 of 3 >> 2 of 3 >> 3 of 3 停止

左键:3 of 3 >> 2 of 3 >> 1 of 3. 停止

但是下面的代码不能正常工作: 请指导我实现逻辑

 class TicketsVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var ticketCount = 1
var currentItem: Int = 0


private var ticketListtData = TicketsListModel(dictionary: NSDictionary()){
    didSet{
        self.currentItem = 0
                    
        if (ticketListtData?.result?.tickets?.count ?? 0) == 0{
            titleLabel.text = ""
            leftBtn.isHidden = true
            rightBtn.isHidden = true
        }
        if (ticketListtData?.result?.tickets?.count ?? 0) == 1{

            titleLabel.text = "1 of 1 Ticket"
            
            leftBtn.isHidden = true
            rightBtn.isHidden = true
        }

        if (ticketListtData?.result?.tickets?.count ?? 0) >= 1{

            titleLabel.text = (ticketListtData?.result?.tickets?.count ?? 0) >= 1 ? "1 of 1 Ticket" : "\(ticketCount) of \(ticketListtData?.result?.tickets?.count ?? 0) Tickets"
            
            leftBtn.isHidden = false
            rightBtn.isHidden = false
        }
        adCollectionView.reloadData()
    }
    }



  @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
}
swift if-statement button uicollectionview
© www.soinside.com 2019 - 2024. All rights reserved.