2016-09-20 23 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再加入其他的邊界不

按下可以清空數組並重新加載集合視圖