如果从UICollectionView中的其他部分中选择一个单元格,请取消选择所有其他选择

时间:2016-12-29 13:02:28

标签: ios swift uicollectionview

如果从第1部分选择任何单元格,如何在第0部分中取消选择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)
    }
}

这不起作用请指导。

4 个答案:

答案 0 :(得分:1)

试试这个。

Swift 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
}

答案 1 :(得分: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)
        }
    }
}

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

答案 2 :(得分:0)

基于Mrugesh的答案,同时考虑到第1部分要求中的单一选择:

Swift 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此功能。

答案 3 :(得分:0)

在代码下方使用以解决问题: - 清除第1部分的tap单元格中的0部分选择。 // didSelect方法

 let indexSet = NSIndexSet(index: 1)
        self.collectionViewBrands.reloadSections(indexSet)