Swift-数组-删除值

时间:2018-06-25 09:30:07

标签: ios arrays swift

这可能是一个简单的问题,但是我正在努力寻找解决方案,但我被阻止了:(

我正在使用CollectionView,该视图显示化身及其个人资料图片的列表。我正在尝试将它们添加到阵列上,然后在取消选择时将其删除。

这是我的代码,我已经尝试了很多东西,但是没有用

选中单元格时的功能

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell2 = collectionV.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! SelectFriendCollectionViewCell
     let cell5 = self.collectionV.cellForItem(at: indexPath) as? SelectFriendCollectionViewCell
    let Friends_avatarname  = (cell5?.avatarUsername_Outlet.text! as? String!)!
    let Friends_UID = (cell5?.avatarUID_Outlet.text! as? String!)!


    let Friend = FriendsSelectArray(avatarUID: (Friends_UID as! String?)!, avatarname: (Friends_avatarname as! String?)!)
    self.FriendsSelect.append(Friend)
    print ("Friend added :\(Friend.avatarname)")

    }

取消选中单元格时的功能

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

        let cell2 = collectionV.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! SelectFriendCollectionViewCell
        let cell5 = self.collectionV.cellForItem(at: indexPath) as? SelectFriendCollectionViewCell


        let Friends_avatarname  = (cell5?.avatarUsername_Outlet.text! as? String!)!
        let Friends_UID = (cell5?.avatarUID_Outlet.text! as? String!)!
        var Friend = FriendsSelectArray(avatarUID: (Friends_UID as! String?)!, avatarname: (Friends_avatarname as! String?)!)

    }

数组的类

class FriendsSelectArray{
    var avatarUID: String
    var avatarname: String


    init( avatarUID: String, avatarname: String){
        self.avatarUID = avatarUID
        self.avatarname = avatarname

    }
    func returnPostAsDictionary()->NSDictionary{
        let postDictionary: NSDictionary = ["avatarUID": avatarUID,
                                            "avatarname": avatarname]
        return postDictionary
    }
}

还有我的CollectionView入门班

class SelectFriendViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionV: UICollectionView!

    var databaseRef: DatabaseReference!
    let cellIdentifier = "cell"
    var FriendsSelect : [FriendsSelectArray] = []
    var selectedCell = [IndexPath]()

当我添加(选择一个单元格时),它可以工作,但是当您取消选择该单元格(您取消选择的特定单元格)时,我无法删除

请,有人可以帮我吗?

先感谢一百万,

2 个答案:

答案 0 :(得分:0)

在模型中设置一个属性,以便它可以随时告诉您是否选择了它。例如:

class FriendsSelectArray{
var avatarUID: String
var avatarname: String
var isSeleted: Bool = false


init( avatarUID: String, avatarname: String){
    self.avatarUID = avatarUID
    self.avatarname = avatarname

}
func returnPostAsDictionary()->NSDictionary{
    let postDictionary: NSDictionary = ["avatarUID": avatarUID,
                                        "avatarname": avatarname]
    return postDictionary
 }

}

然后在isSeleted = true项目中设置didselect,并在isSelected = false方法中设置itemDidDeselect。之后,您可以轻松地从array中的其余项目中过滤所选项目。

答案 1 :(得分:0)

您应该将列表中删除的朋友的ID与所有朋友的ID进行比较,然后从数组中删除所需的ID,然后重新加载集合视图。

请查看以下代码和代码中的注释:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

        let cell2 = collectionV.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! SelectFriendCollectionViewCell
        let cell5 = self.collectionV.cellForItem(at: indexPath) as? SelectFriendCollectionViewCell


        let Friends_avatarname  = (cell5?.avatarUsername_Outlet.text! as? String!)!
        let Friends_UID = (cell5?.avatarUID_Outlet.text! as? String!)!
        var Friend = FriendsSelectArray(avatarUID: (Friends_UID as! String?)!, avatarname: (Friends_avatarname as! String?)!)

        // Pseudocode, remove friend from array logic.
        // Compare avatarUID of friend being removed and friends in the list.
        for x in (0..<FriendsSelect.count).reversed() {
             if(FriendsSelect[x].avatarUID == Friend.avatarUID) {
                 FriendsSelect.remove(at: x)
                 break
             }
        }

        // TODO: Reload array
    }