2016-12-29 116 views
0

如何取消选择所有其他选定的细胞UICollectionView在第0,如果从第1节
选择以下任一单元格是我试过到目前为止:取消选择所有其他选择,如果从其他部分选择一个单元格中UICollectionView

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

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! SignupStep3CollectionViewCell 

    cell.layer.borderWidth = 4 
    cell.layer.masksToBounds = false 
    cell.layer.borderColor = UIColor.init(red: 46.0/255.0, green: 234.0/255.0, blue: 219.0/255.0, alpha: 1.0).CGColor 
    cell.layer.cornerRadius = cell.frame.height/2 
    cell.clipsToBounds = true 

    if indexPath.section == 1 
    { 
     collectionView.selectItemAtIndexPath(nil, animated: true, scrollPosition: .None) 
    } 
} 

这不工作请引导。

回答

0

您可以实现以下的委托方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let section = indexPath.section 
    let selectedRows = collectionView.indexPathsForSelectedItems 
    for i in 0...(selectedRows!.count - 1) { 
     let path = selectedRows![i] as IndexPath 
     if(path.section != section) { 
      collectionView.deselectItem(at: path, animated: false) 
     } 
    } 
} 

在选择任何特定的项目,你可以得到所有选定的项目。然后取消选择来自不同部分的所有项目。

+0

“NSIndexpath不confrm以协议序列类型”的错误来 –

+0

更新了答案。你现在可以检查吗? –

+0

仍然错误,使用swift 2 –

1

试试这个。

夫特3

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 

    //multiple selection in section 0 otherwise single selection in other section 
    if indexPath.section == 0 { 
     return true 
    } 
    let indexPaths = collectionView.indexPathsForSelectedItems 
    if (indexPaths?.count) ?? 0 > 0 { 

     /// If you need simple way 
     for index in indexPaths! { 
     if index.section == indexPath.section { 
      self.collectionView.deselectItem(at: index, animated: true) // if want deselect previous selection 
      //return false //if you do not want further selection 
      } 
     } 

     /// If you need some optimization and don't want to run loop each time 
     /* 
     let arrIndexPaths = NSArray(array: indexPaths!) 
     let sectionPrediate = NSPredicate(format: "section == %d", indexPath.section) 
     let arrSelections = arrIndexPaths.filtered(using: sectionPrediate) as? [IndexPath] 
     if arrSelections?.count ?? 0 > 0 { 
      self.collectionView.deselectItem(at: arrSelections![0], animated: true) // if want deselect previous selection 
      //return false //if you do not want further selection 
     }*/ 
    } 

    return true 
} 

SWIFT 2.3

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool { 
    //multiple selection in section 0 otherwise single selection in other section 
    if indexPath.section == 0 { 
     return true 
    } 
    let indexPaths = collectionView.indexPathsForSelectedItems() 
    if (indexPaths?.count) ?? 0 > 0 { 

     /// If you need simple way 
     for index in indexPaths! { 
      if index.section == indexPath.section { 
       self.colletionView.deselectItemAtIndexPath(index, animated: true) // if want deselect previous selection 
       //return false //if you do not want further selection 
      } 
     } 

     /// If you need some optimization and don't want to run loop each time 
     /*let arrIndexPaths = NSArray(array: indexPaths!) 
     let sectionPrediate = NSPredicate(format: "section == %d", indexPath.section) 
     let arrSelections = arrIndexPaths.filteredArrayUsingPredicate(sectionPrediate) as? [NSIndexPath] 
     if arrSelections?.count ?? 0 > 0 { 
      self.colletionView.deselectItemAtIndexPath(arrSelections![0], animated: true) // if want deselect previous selection 
      //return false //if you do not want further selection 
     }*/ 
    } 

    return true 
} 
+0

“NSIndexpath不符合协议序列类型”出错 –

+0

您使用Swift3吗?因为这是工作正常与Xcode 8.1 –

+0

没有使用SWIFT 2 –

0

基于关Mrugesh的答案,但也考虑到单个选择在第1个要求:

夫特2:

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool { 
    let indexPaths = collectionView.indexPathsForSelectedItems() 
    if indexPaths?.count ?? 0 > 0 { 
     if indexPaths![0].section != indexPath.section || indexPath.section == 1 { 
      for index in indexPaths! { 
       collectionView.deselectItemAtIndexPath(index, animated: true) 
      } 
     } 
    } 
    return true 
} 

斯威夫特3:

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
    let indexPaths = collectionView.indexPathsForSelectedItems 
    if (indexPaths?.count) ?? 0 > 0 { 
     if indexPaths![0].section != indexPath.section || indexPath.section == 1 { 
      for index in indexPaths! { 
       collectionView.deselectItem(at: index, animated: true) 
      } 
     } 
    } 
    return true 
} 

根据您的视图控制器是否是UICollectionViewController一个子类,或只是实现它的委托和数据源的协议,您可能需要override此功能。

+0

它只允许我进行第0部分多重选择启用和第1部分单一选择。但如果选择了第1部分的任何单元格,则不会清除第0部分的选择。 –

+0

这很奇怪。它适用于具有集合视图的简单应用程序,使用'cell.selected'来确定单元格应具有的背景颜色。 'indexPathsForSelectedItems'也打印正确的选择。我不能同时选择第0部分和第1部分。你如何确定选择什么? – Samantha

0

使用下面的代码来解决这个问题: - 明确的部分0的选择上部分的拼接单元1 // didSelect方法

let indexSet = NSIndexSet(index: 1) 
     self.collectionViewBrands.reloadSections(indexSet) 
相关问题