2017-04-17 77 views
2

制作后,我一直都有点吃力用于储蓄,而该细胞被标记和没有,这个问题并不难不让被选中。经过几次尝试,所有关于保存indePath的地方,回到视图与收集后,我们能够做arrayarrayindexPathscollectionView方法willDisplay内的一些东西。基本上,它看起来如何:细胞采集它选择

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
     switch collectionView { 
     case myCollection: 
      var count: Int = 0 
      if !SessionMenager.Instance.indexPaths.isEmpty { 
       for index in SessionMenager.Instance.indexPaths { 
        if index == indexPath { 
         print(SessionMenager.Instance.indexPaths.count) 
         myCollection.selectItem(at: SessionMenager.Instance.indexPaths[count], animated: true, scrollPosition: .bottom) 
         var image = cell.viewWithTag(1) as! UIImageView 
         UIView.animate(withDuration: 0.25, animations: { 
          image.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
          image = image.drawRingForCircle(imageView: image, color: .white) 
         }) 
         cell.isSelected = true 
         count += 1 
        } 
       } 
      } 
     default: 
      break 
     } 
    } 

这是完美的,但只为一种方式。现在我无法取消选择除去第一个选定的单元格之前的任何其他单元格view - 为什么?有人能帮助我吗?

提前致谢!

编辑

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath { 

switch collectionView { 

case xxx: 
     break 
case yyy: 
     break 
     case myCollection: 
      let image = myCollection.cellForItem(at: indexPath)?.viewWithTag(1) as! UIImageView 

      UIView.animate(withDuration: 0.25, animations: { 
       image.transform = CGAffineTransform(scaleX: 1, y: 1) 
       image.layer.sublayers?.removeLast() 
      }) 

      for (key, _) in tmpDict { 
       if key == image { 
        if let index = self.choosenData.index(of: tmpDict[key]!) { 
         self.choosenData.remove(at: index) 
         SessionMenager.Instance.indexPaths = SessionMenager.Instance.indexPaths.filter { $0 != indexPath } 
        } 
       } 
      } 
     default: 
      break 
     } 
} 
+0

嘛,怎么是你想取消细胞?您只发布了选择部分的代码。 –

+0

@PedroCastilho它没有进入方法'didDeselectItemAt'为其他细胞,然后第一个保存。但是,请雇用你! – yerpy

+0

为什么'switch'语句? –

回答

0

ANSWER

我fugure了必须做什么,使其工作! 基本上每当有人会有类似的问题,我不建议willDisplay cell方法 - 它不会很好地工作。 当你在这个方法中收集调用.selectItem,细胞是的,它会被选中,但..有一个问题,即使是在u盘上这些选择后,U中的委托一格内按一下就会跳入didSelectItemAt,而不是didDeselectItemAt所以要取消选中你需要的单元格两次,这是不好的。

,我们可能增加willDisplay cell.isSelected = true但在那之后我们解决了问题的~50% becouse我们不能再取消他们的第二件事情,委托没有任何反应多为其他选定的单元格,而不是第一选择。

我问我自己,如果我的一切后做同样的东西装?然后我尝试了它 - 它的工作原理!

对于我用viewDidAppear(_ animated: Bool)

工作示例如下:

if !SessionMenager.Instance.indexPaths.isEmpty { 
      for index in SessionMenager.Instance.colorsIndexPaths { 
       let cell = myCollection.cellForItem(at: index)! 
       myCollection.selectItem(at: index, animated: true, scrollPosition: .bottom) 
       var something = cell.viewWithTag(**YOUR TAG**) as! SMTH 

       // And the body what would you like to do with `something`, ex. increase scale of item, change a background, tint color etc. 

       }) 
      } 
     } 

希望这将帮助任何人谁需要的!干杯!

0

如何改变你的collectionView(_:willDisplay:forItemAt:)方法是:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    var count: Int = 0 
    for index in SessionMenager.Instance.indexPaths { 
     if index == indexPath { 
      myCollection.selectItem(at: indexPath, animated: true, scrollPosition: .bottom) 
      var image = cell.viewWithTag(1) as! UIImageView 
      UIView.animate(withDuration: 0.25, animations: { 
       image.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) 
       image = image.drawRingForCircle(imageView: image, color: .white) 
      }) 
     } 
    } 
} 

也许不能解决你的问题,但它肯定会更简单调试。另外,您不需要直接在您的手机上设置isSelectedUICollectionView.selectItem已经做到了。

你也应该检查你是否得到所有正确的IndexPath s,并在完成加载数据后在你的集合视图中调用UICollectionView.reloadData

+0

'isSelected'是需要的因为不知何故,如果你不这样做,单元格认为它还没有被选中,即使我已经改变了视图等,它需要点击两次以取消选择,所以在第一次它去了选择,然后第二次取消选择:D werid行为 - alrady调试。路径也是正确的。我制定了一个算法让他们保持良好状态。 – yerpy

+0

它不应该被需要。从苹果公司的文档:[“你通常不直接设置该属性的值。以编程方式更改此属性的值不会更改单元格的外观。选择单元格并突出显示它的首选方法是使用选择方法的集合视图对象。“](https://developer.apple.com/reference/uikit/uicollectionviewcell/1620130-isselected) –

+0

我同意你的意见,但不知何故不能像他们说的那样工作。虽然我把选定的项目,它应该去选择第一步选择的项目后,“didSelectItemAt”,但“didDeselectItemAt” – yerpy