UICollection的IndexPath到底是什么 [英] What exactly is the IndexPath of a UICollection

查看:198
本文介绍了UICollection的IndexPath到底是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用UICollectionViews时遇到两个问题,我不知何故在这两种情况下都与IndexPath有关,但是我真的不知道何时调用Indexpath或它到底在做什么,以及何时更新或更改它。

无论如何,这是我的问题:
1.索引路径以某种方式多次为0? Youtube VIDEO显示行为

I'm having 2 problems with UICollectionViews, and I somehow believe it's related to the IndexPath in both cases, however I don't really know when is the Indexpath called or what exactly it does and when it gets updated or changed.
Anyways, here are my issues: 1. Somehow the Indexpath is 0 multiple times?Youtube VIDEO showing behavior

对此行为的特殊代码如下:

for this particular behavior here is the code:

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.ptsRemaining = pointsToAllocate
        cell.ptsUsed = abs(pointsUsedInCells[indexPath.row])
        if indexPath.row == 0 {
            cell.isSelf = true
            cell.layer.borderWidth = 5
            cell.layer.borderColor = UIColor.blue.cgColor
        } else {
            cell.isSelf = false
        }
        cell.updatedButtons()
        return cell
    }

如您所见,仅在INDEXPATH.ROW == 0处,单元格应该有边框(因为那是当前用户)。但是,当我单击按钮(请参见下面的按钮逻辑)时,它们突然都变成了索引路径== 0?

As you can see ONLY AT INDEXPATH.ROW == 0 should the cell have a border (because thats' the current user). However when I click the buttons (see button logic below) suddenly they're all index path == 0?

class PointAllocCell: UICollectionViewCell {
    var isSelf: Bool = false
    var cellIndex: Int?
    var ptsRemaining: Int = 0
    var ptsUsed: Int = 0
    var cellDelegate: PointsAllocDelegate?

    @IBAction func plusTapped(_ sender: Any) {
        if cellDelegate != nil {
            cellDelegate!.plusTapReported(fromCell: cellIndex!)
        }
        updatedButtons()
    }

    @IBAction func minusTapped(_ sender: Any) {
        if cellDelegate != nil {
            cellDelegate!.minusTapReported(fromCell: cellIndex!)
        }
        updatedButtons()
    }

    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
        }
        pointLabel.text = String(ptsUsed)
    }
}

最后代表:

extension PointAllocVC: PointsAllocDelegate {
    func plusTapReported(fromCell: Int) {
        if fromCell == 0 {
            //this is self
            pointsToAllocate -= 1
            pointsUsedInCells[fromCell] += 1
        } else {
            pointsToAllocate += 1
            pointsUsedInCells[fromCell] += 1
        }
        reloadCollection()
        reloadLabels()
    }

    func minusTapReported(fromCell: Int) {
        if fromCell == 0 {
            //this is self
            pointsToAllocate += 1
            pointsUsedInCells[fromCell] -= 1
        } else {
            pointsToAllocate -= 1
            pointsUsedInCells[fromCell] -= 1
        }
        reloadCollection()
        reloadLabels()
    }
}

现在。第二个问题,当我执行以下操作时得到提示

Now. The SECOND ISSUE, I'm getting it when I do the following

playerArray.remove(at: gKPlayerArray.index(of: playerDed)!)

这行代码没有任何作用,但是在我调用 reloadData我得到IndexPath.row超出范围(出于某种原因,即使我在调用它之前删除了一个项目,它仍然认为playerArray的大小为X + 1。

This line of code doesn't do anything, however AFTER that when I call the "reloadData" I get that the IndexPath.row is out of range (for some reason it still thinks that the playerArray is a size X+1 even though I removed an item before calling it.

推荐答案

一个单元永远不需要知道它自己的indexPath。这是一个非常糟糕的设计。

A cell never needs to know its own indexPath. That's a a really bad design.

重构代码,以便您不会将行或indexPath传递给单元格。

Refactor your code so you do not pass a row or indexPath to a cell.

更改您的单元格委托协议以传递实际的单元格,而不是 Int

Change your cell delegate protocol to pass the actual cell, not an Int.

现在将告知实际实现委托方法的类,以代表该委托方法被调用的单元格,并且如果需要,该类可以确定单元格的indexPath。

The class that actually implements the delegate methods will now be told which cell the delegate method is being called on behalf of and if needed, that class can determine the indexPath of the cell.

您还需要更新您的 cellForItemAt

此代码是一个问题:

    if indexPath.row == 0 {
        cell.isSelf = true
        cell.layer.borderWidth = 5
        cell.layer.borderColor = UIColor.blue.cgColor
    } else {
        cell.isSelf = false
    }

无论何时设置属性,都必须始终将其重置为其他条件。

Whenever you set a property, you must always reset it for the other condition.

    if indexPath.row == 0 {
        cell.isSelf = true
        cell.layer.borderWidth = 5
        cell.layer.borderColor = UIColor.blue.cgColor
    } else {
        cell.isSelf = false
        cell.layer.borderWidth = 0
    }

您不不需要为宽度为0的边框重置颜色。

You don't need to reset the color for a 0 width border.

这篇关于UICollection的IndexPath到底是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆