【问题标题】:didSelectItemAt and didDeselectItemAt are not working as expected in swift 3.0didSelectItemAt 和 didDeselectItemAt 在 swift 3.0 中没有按预期工作
【发布时间】:2017-12-21 13:34:41
【问题描述】:

我正在使用 didSelectItemAt 和 didDeselectItemAt 来选择 collectionViewCell。我想选择单元格并将边框设为蓝色(如果已选中)并取消选择“选定”单元格并将边框设为默认值。但我的问题是 didDeselectItemAt 被交替调用。当我点击任何单元格然后调用 didSelectItemAt 并且如果我点击任何其他单元格然后调用 didDeselectItemAt。我猜这不应该发生。仅当我点击已选择的单元格时才应调用 didDeselectItemAt。如果我错了,请纠正我。我已经提到了这个UICollectionView - didDeselectItemAtIndexPath not called if cell is selected1,但是对我有用:(

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
   let cell = collectionView.cellForItem(at: indexPath) as!  MomentDetailCell
   let moment = self.arrOfMoments[indexPath.row] as! MomentModel
   cell.toggleSelection(moment: moment)
   self.arrOfDeletingImgs.append(moment) 
}

public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
   let cell : MomentDetailCell = self.collectionViewImages.cellForItem(at: indexPath) as! MomentDetailCell
   let moment = self.arrOfMoments[indexPath.row] as! MomentModel
   cell.toggleSelection(moment: moment)
   self.arrOfDeletingImgs.remove(at: (find(objecToFind: moment))!)
}

// 这也是我在课堂上使用的代码。我还在 viewdidload 中设置了allowMultipleSelection true

 extension MomentDetailViewController : UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
      return CGSize(width: 75, height: 75)
    }
}

//这是我的customCell代码

func toggleSelection( moment : MomentModel)
{
    if (isSelected)
    {
        moment.isSelected = true
        self.layer.borderWidth = 3
        self.layer.borderColor = Constant.APP_BLUE_COLOR.cgColor

    }
    else
    {
        moment.isSelected = false
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.red.cgColor
    }
}

【问题讨论】:

  • 我猜当您点击第二个单元格时会调用 didselect 方法,因此首先会被取消选择。您再次点击第一个并保持选中状态,您是否检查过它是否转到其他单元格?您需要将选定的索引状态保存在某处。

标签: ios objective-c swift3 uicollectionviewcell


【解决方案1】:

试试这个:

这种多选解决方案

1- 取消选择如下

 public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
       let cell = collectionView.cellForItem(at: indexPath) as!  MomentDetailCell
       let moment = self.arrOfMoments[indexPath.row] as! MomentModel
       cell.toggleSelection(moment: moment)
       self.arrOfDeletingImgs.append(moment) 
    }

2 - 注释/删除取消选择方法

3改变这个方法

func toggleSelection( moment : MomentModel)
{
   moment.isSelected = !moment.isSelected
   self.layer.borderWidth =  moment.isSelected ? 3 : 1
   self.layer.borderColor =  moment.isSelected ?  Constant.APP_BLUE_COLOR.cgColor :  UIColor.red.cgColor

}

【讨论】:

  • 您的答案没有有效的变化。您刚刚使用三元运算符而不是 if else 优化了代码。
  • 我也提到了删除你的 didDeselectItemAt 方法。
  • 好的,谢谢。无论如何,它不会起作用,因为我通过引用Answer 上的其他答案为 cellforItem 中的每个单元格设置了 selction false,我很遗憾地混合了各种答案。
【解决方案2】:

经过很长时间我解决了问题,这对我有用...

我在 cellForItemAtIndexPath 中做了以下事情:这是 WRONG

cell.isSelected = false
collectionView.selectItem(at: indexPath, 动画: false, scrollPosition: .left)

我这样做是因为 didDeselect 被交替调用而不考虑单元格的选择。我刚刚取消了这段代码,它对我有用。现在 didSelect 在第一次点击时调用,如果我再次点击同一个单元格,那么只有 didDeselect 会按照预期的流程被调用。

还要确保allowsMultipleSelection 为真并且allowsSelection 为真

【讨论】:

    【解决方案3】:

    我猜当你点击第二个单元格时会调用 didselect 方法。你可能忘记了:

    _collectionView.allowsMultipleSelection = YES
    

    【讨论】:

      【解决方案4】:

      在我的情况下,我有一个单独的委托类,如果我设置 collectionView.delegate = MyDelegate(),它就不起作用,如果我在我的视图控制器中存储一个 var myDelegate = MyDelegate(),然后设置 collectionView.delegate = myDelegate 它以某种方式起作用。

      【讨论】:

        猜你喜欢
        • 2019-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        • 1970-01-01
        相关资源
        最近更新 更多