【发布时间】:2018-01-30 16:33:01
【问题描述】:
我在使用 UICollectionViews 时遇到了 2 个问题,并且我以某种方式相信它在这两种情况下都与 IndexPath 相关,但是我真的不知道何时调用 IndexPath 或它究竟做了什么以及何时更新或更改。
无论如何,这是我的问题:
1. 不知何故Indexpath多次为0?Youtube VIDEO showing behavior
对于这种特殊行为,代码如下:
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?
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()
}
}
现在。第二个问题,当我执行以下操作时,我得到了它
playerArray.remove(at: gKPlayerArray.index(of: playerDed)!)
这行代码没有做任何事情,但是当我调用“reloadData”之后,我发现 IndexPath.row 超出范围(由于某种原因,它仍然认为 playerArray 的大小为 X+1即使我在调用它之前删除了一个项目。
【问题讨论】:
-
关于您的第一个问题,您还必须在
else正文中明确设置边框,因为单元格被重复使用。
标签: ios arrays swift uicollectionview