2016-09-20 24 views
0

我有集合视图具有两个海关细胞,我有一个方法,该方法选择的细胞的边界出现在选定的一些情况下,并且取消所有选择的按钮,问题是当按下按钮时,单元格边框被设置为清除颜色,但是当再次进入选择模式时,先前选择的单元格仍然有边框,所以我要找的是当我取消选择然后再次进入选择模式时,没有单元格应该是这里是代码:UIcollection查看多个选择取消细胞

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if isGridSelected { 

     let cell:cell2_Class = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cell2_Class 

     collectionView.allowsMultipleSelection = true 
     collectionView.allowsSelection = true 

     cell.listImage.image = imageArray[indexPath.row] 

     if self.selectedIndexes.indexOf(indexPath) == nil || cancel == true { 
      cell.layer.borderColor = UIColor.clearColor().CGColor 

      // Unselected 
     } 
     else if cancel == true { cell.layer.borderColor = UIColor.clearColor().CGColor 
      for indexPath: NSIndexPath in selectedIndexes { 

       self.collectionView.deselectItemAtIndexPath(indexPath, animated: false) 
       collectionView.reloadData() 
      } 
     } else { 
      cell.layer.borderColor = UIColor.greenColor().CGColor // Selected 
      cell.layer.borderWidth = 3 
     } 

     return cell 
    } 
} 

var flag = false 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    if flag == false{ self.performSegueWithIdentifier("showimage", sender: self) } 

    else if cancel == true { 

     let cell = collectionView.cellForItemAtIndexPath(indexPath) 
     cell?.selected = false 

     // deselect 
    } else if flag == true { 

     if let indexSelection = selectedIndexes.indexOf(indexPath) { 
      selectedIndexes.removeAtIndex(indexSelection) 

     } else { 
      selectedIndexes.append(indexPath) 
     } 

     self.collectionView.reloadData() 

    } 
} 

func handleLongPress(gestureReconizer: UILongPressGestureRecognizer)  { 
    if gestureReconizer.state != UIGestureRecognizerState.Began { 
     return 
    } 

    let p = gestureReconizer.locationInView(self.collectionView) 
    let indexPath = self.collectionView.indexPathForItemAtPoint(p) 

    if indexPath != nil { 

     if let indexSelection = selectedIndexes.indexOf(indexPath!) { 
      selectedIndexes.removeAtIndex(indexSelection) 
     } else { 
      selectedIndexes.append(indexPath!) 
     } 
     print(indexPath?.row) 
     cancel = false 
     flag = true 
     self.title = "share photos" 
     cancelButton.hidden = false 

    } else { 
    } 
} 
+0

仍然在其工作时取消,谢谢:) –

回答

0

它看起来像当你按cancel你拨打self.collectionView.deselectItemAtIndexPath(indexPath, animated: false)所有indexPathsself.selectedIndexPaths,把我没有看到你的代码中的任何地方,这删除他们从self.selectedIndexPaths,是吗?

相反试试这个

else if cancel == true { 
    self.selectedIndexes.removeAllObjects() 
    collectionView.reloadData()   
} 

我也必须指出,UICollectionView设有私人variable称为indexPathsForSelectedItems: [IndexPath]?可用于这种类型的实现,并与在UICollectionViewCell压倒一切的setSelected()组合可以使你的代码地段lighter。个人喜好。

+0

非常感谢你,我工作的它 –

0

我是没有那么多的使用对迅速,但其所有的逻辑,这样我就可以给你的想法

  1. 添加任何东西从删除对象之前创建一个NSMutableArray的
  2. 在didSelectRow方法添加indexPath
  3. 该数组(使用次数来获得对象的数量)
  4. 重装该表

注:CE添加检查如果llForRow方法数组包含indexPath再加入其他的边界不

按下可以清空数组并重新加载集合视图