分配点:按钮不更新

时间:2018-01-29 10:07:36

标签: ios swift xcode uicollectionview

我有一个点分配"屏幕,您可以为自己添加点数或从敌人中减去它们。根据您制作的点数,您可以在转弯结束时显示此信息。这是显示分配屏幕的代码:

extension PointAllocVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (playerArray?.count)!
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "playerCell", for: indexPath) as! PointAllocCell
        cell.cellDelegate = self
        cell.cellIndex = indexPath.row
        cell.nameLabel.text = playerArray![indexPath.row].alias
        cell.scoreLabel.text = String(currentScore[indexPath.row])
        cell.minusBtn.layer.cornerRadius = 15
        cell.plusBtn.layer.cornerRadius = 15
        cell.minusBtn.isEnabled = false
        cell.plusBtn.isEnabled = false
        if indexPath.row == 0 {
            cell.isSelf = true
        }
        cell.updatedButtons()
        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 130, height: 130);
    }
}

然后这是集合视图中每个单元格的代码:

   @IBAction func plusTapped(_ sender: Any) {
        if isSelf {
            ptsUsed += 1
            ptsRemaining = cellDelegate?.pointUsed()
            updatedButtons()
        } else {
            ptsUsed -= 1
            ptsRemaining = cellDelegate?.pointReturned()
            updatedButtons()
        }
        pointLabel.text = String(ptsUsed)
        cellDelegate?.updateCellScores(index: cellIndex!, score: ptsUsed)
        cellDelegate?.updateCollection()
    }

    @IBAction func minusTapped(_ sender: Any) {
        if isSelf {
            ptsUsed -= 1
            ptsRemaining = cellDelegate?.pointReturned()
            updatedButtons()
        } else {
            ptsUsed += 1
            ptsRemaining = (cellDelegate?.pointUsed())!
            updatedButtons()
        }
        pointLabel.text = String(ptsUsed)
        cellDelegate?.updateCellScores(index: cellIndex!, score: ptsUsed)
        cellDelegate?.updateCollection()
    }

    func updatedButtons() {
        switch (ptsRemaining, ptsUsed) {
        case (0?,0):
            plusBtn.isEnabled = false
            minusBtn.isEnabled = false
        case (0?,_):
            if isSelf{
                plusBtn.isEnabled = false
                minusBtn.isEnabled = true
            } else {
                plusBtn.isEnabled = true
                minusBtn.isEnabled = false
            }
        case (_,0):
            if isSelf{
                plusBtn.isEnabled = true
                minusBtn.isEnabled = false
            } else {
                plusBtn.isEnabled = false
                minusBtn.isEnabled = true
            }
        default:
            plusBtn.isEnabled = true
            minusBtn.isEnabled = true
        }
    }

最后是委托方法:

extension PointAllocVC: PointsAllocDelegate {
    func pointUsed() -> Int {
        pointsToAllocate -= 1
        return pointsToAllocate
    }

    func pointReturned() -> Int {
        pointsToAllocate += 1
        return pointsToAllocate
    }

    func updateCellScores(index: Int, score: Int) {
        if index != 0 {
            pointsUsedInCells[index] = -score
        } else {
            pointsUsedInCells[index] = score
        }
    }

    func updateCollection() {
        playerCollectionView.reloadData()
        reloadLabels()
    }
}

无论如何,问题是我在以下场景中只会产生一种非常奇怪的行为(这种情况太常见了):

假设您有X点要分配。你唯一的选择是你自己的加号按钮或对手的减号按钮(此时它有效) 现在说你给自己添加X点。你剩下0分,你身边的加号按钮被禁用(应该如此)。然而,对手" CELL"即使我要求整个集合更新,也没有更新。 如果你玩它,你突然现在有-x点分配(这不应该是可能的)。 然后,如果你继续玩它完全禁用对手的细胞,你再也不能做任何事了。 这是一个显示行为的视频: https://youtu.be/SPGxzL7ESwc

0 个答案:

没有答案