Arrays 取消选择集合视图中的项目并将其从不工作的数组中删除

Arrays 取消选择集合视图中的项目并将其从不工作的数组中删除,arrays,swift,uicollectionview,Arrays,Swift,Uicollectionview,我正在尝试选择一个单元格,并将该单元格的内容添加到一个数组中,然后能够取消选择一个单元格并从数组中删除相同的对象。我已经挣扎了几天,但似乎仍然无法让它工作。“选择”和“取消选择”功能起作用,它将对象添加到数组中,但取消选择后,我不知道如何从数组中删除相同的对象。如何取消选择单元格并将其从数组中删除 这是我的代码: struct Friends { var friendName : String! } struct SelectedMembers { var select

我正在尝试选择一个单元格,并将该单元格的内容添加到一个数组中,然后能够取消选择一个单元格并从数组中删除相同的对象。我已经挣扎了几天,但似乎仍然无法让它工作。“选择”和“取消选择”功能起作用,它将对象添加到数组中,但取消选择后,我不知道如何从数组中删除相同的对象。如何取消选择单元格并将其从数组中删除

这是我的代码:

    struct Friends {
    var friendName : String!
}

struct SelectedMembers {
    var selectedFriend : String!
}


    var friends = [Friends]()
    var selectedFriends = [SelectedMembers]()

        //MARK: Select multiple cells
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? FriendsCell
        selectedCell.append(indexPath)
        cell?.contentView.backgroundColor = .red
        print("Friends are: \(String(describing: cell?.nameSurname.text!))")

        if (cell?.isSelected)! {
            print("this is selected...")
            var selection = SelectedMembers()
            selection.selectedFriend = friends[indexPath.item].friendName
            selectedFriends.append(selection)

            print(selectedFriends)

        }




    }

    //MARK: Deselect Multiple cells
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let deselectedCell = collectionView.cellForItem(at: indexPath) as? FriendsCell
        print(indexPath.row)
        if let index = selectedFriends.index(of: selectedFriends[indexPath.row]) {
            selectedFriends.remove(at: index)
        }

        if selectedCell.contains(indexPath) {
            selectedCell.remove(at: selectedCell.index(of: indexPath)!)
            deselectedCell?.contentView.backgroundColor = .white
            print(selectedCell)
        }


        print(selectedFriends)
    }
}

这段代码就是罪魁祸首

如果让index=selectedFriends.index(of:selectedFriends[indexPath.row]){selectedFriends.remove(at:index)}

例如,如果您有100行,并且选择了1、10、50个单元格,则您的selectedFriends数组仅包含3个对象,但当有人取消选择单元格中的50行时,在上面的代码中,您试图访问selectedFriends[50]…因此这是不正确的

您不需要维护selectedCell数组来跟踪选中/未选中的索引。无论何时选择单元格,您都会在didSelecte中收到一个回调,只有选定的单元格才能取消选择。

将下面的代码放在DIDDENSELECT()中

如果您的朋友列表更多,最好使用字典,这样您就可以避免for循环来查找取消选择的朋友。实现Equatable协议,而不是在执行阵列实现时检查唯一的名称


这段代码就是罪魁祸首

如果让index=selectedFriends.index(of:selectedFriends[indexPath.row]){selectedFriends.remove(at:index)}

例如,如果您有100行,并且选择了1、10、50个单元格,则您的selectedFriends数组仅包含3个对象,但当有人取消选择单元格中的50行时,在上面的代码中,您试图访问selectedFriends[50]…因此这是不正确的

您不需要维护selectedCell数组来跟踪选中/未选中的索引。无论何时选择单元格,您都会在didSelecte中收到一个回调,只有选定的单元格才能取消选择。

将下面的代码放在DIDDENSELECT()中

如果您的朋友列表更多,最好使用字典,这样您就可以避免for循环来查找取消选择的朋友。实现Equatable协议,而不是在执行阵列实现时检查唯一的名称


根据我的说法,在“SelectedMembers”对象中创建一个isSelected键,并更改选定和未选定单元格的键值,然后重新加载tableView。 最后,您将通过简单循环检索所选单元格的数组

其他明智的使用它

 extension SelectRecipeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let product =  self.productList[indexPath.row]
    if arraySelectedProduct.contains(recipe) {
        let index = arraySelectedRecipes.index(of: recipe)
        arraySelectedProduct.remove(at: index!)
    } else {
        arraySelectedProduct.append(recipe)
    }
    tableView.reloadRows(at: [indexPath], with: .none)
}
}

根据我的说法,在“SelectedMembers”对象中创建一个isSelected键,并更改选定和未选定单元格的键值,然后重新加载tableView。 最后,您将通过简单循环检索所选单元格的数组

其他明智的使用它

 extension SelectRecipeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let product =  self.productList[indexPath.row]
    if arraySelectedProduct.contains(recipe) {
        let index = arraySelectedRecipes.index(of: recipe)
        arraySelectedProduct.remove(at: index!)
    } else {
        arraySelectedProduct.append(recipe)
    }
    tableView.reloadRows(at: [indexPath], with: .none)
}
}

谢谢你的回答!当for循环发生时,我遇到了这个错误:“表达式类型(SelectedMembers)在没有更多上下文的情况下是不明确的”。也是一个整数?var deselectedIndex=Int()?我应该这样放吗?嗨,库马尔!我仍然会遇到这个错误:二进制运算符“==”不能应用于“SelectedMembers”和“String”类型的操作数。别担心,我总是标记答案,只是我还没有让它工作。谢谢你的回答!当for循环发生时,我遇到了这个错误:“表达式类型(SelectedMembers)在没有更多上下文的情况下是不明确的”。也是一个整数?var deselectedIndex=Int()?我应该这样放吗?嗨,库马尔!我仍然会遇到这个错误:二进制运算符“==”不能应用于“SelectedMembers”和“String”类型的操作数。别担心,我总是标记答案,只是我还没有让它工作。