UICollectionview将数据附加到Array Swift

时间:2018-05-30 02:28:59

标签: ios swift uitableview uicollectionview

我有一个群组聊天应用程序,只要我点击该用户,我就可以将用户添加到群组中。组成员选择页面如下所示:

enter image description here

每当我点击一个用户时,它都会被添加到顶部的collectionView中。我的collectionView位于顶部,而tableview位于底部。 一旦我点击用户,它看起来像:

enter image description here

我有一个错误,即无论我点击哪里都没关系,即使我点击第二行,用户测试人员2或第三行,用户apple,collectionView向我显示第一个用户,然后它显示第二个用户附加到它和第三和第四。我应该如何向用户显示我点击的用户?

我的collectionView:

func collectionView(_ collectionView: UICollectionView, 
numberOfItemsInSection section: Int) -> Int {
    return chosenUser.count
}

func updateCollectionView() {
    DispatchQueue.main.async {
        self.collectionView.reloadData()
    }
}

var chosenUser: [User] = []

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ChosenUserCollectionViewCell

    let user = users[indexPath.row]

    cell.chosenUserImage.layer.cornerRadius = 30
    cell.chosenUserImage.layer.masksToBounds = true
    cell.chosenUserImage.contentMode = .scaleAspectFill

    if let profileImageUrl = user.profileImageUrl {
        cell.chosenUserImage.loadImageUsingCacheWithUrlString(profileImageUrl)
    } else {
        cell.chosenUserImage.image = UIImage(named: "profileIcon")
    }

    return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return chosenUser.count
}

我的桌面视图:

protocol toRefresh {
func updateCollectionView()}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let user = users[indexPath.row]
    self.chosenUser.append(user)
    updateCollectionView()
    }
}

我可以知道我在我的tableview DidSelectRowAt上做了什么错误吗?

1 个答案:

答案 0 :(得分:1)

我认为在您的collectionView cellForRow 应该使用 ChoosenUser 数组,而不是您用来填充tableView的主阵列用户

您的工作流程

TableView中的

DidSelect您在self.chosenUser数组中附加Selected User哪个是正确的

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let user = users[indexPath.row]
    /// appending user
    self.chosenUser.append(user)
    updateCollectionView()
    }
}

现在来到collectionView - 右键,您返回了所选的用户阵列计数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return chosenUser.count
}

现在谈到集合ViewView CellForRow中的Dequeue用户

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ChosenUserCollectionViewCell

    /// Here You did The mistake - 
    /// You did used main Array **Users**
    let user = users[indexPath.row]

    /// You actually need
    let user = choosenUser[indexPath.row] 

    /// What's Happening?
    /// You selected one user from tableView Appended it to newArray
    /// You Setted collectionView count according to new Array
    /// But you used main Array
    /// ---------------------Example-----------------
    ///Case -> Count is 1 - First user from mainArray will be shown
    /// Case -> count is 2 - First two user from mainArray Users will be shown
    cell.chosenUserImage.layer.cornerRadius = 30
    cell.chosenUserImage.layer.masksToBounds = true
    cell.chosenUserImage.contentMode = .scaleAspectFill

    if let profileImageUrl = user.profileImageUrl {
        cell.chosenUserImage.loadImageUsingCacheWithUrlString(profileImageUrl)
    } else {
        cell.chosenUserImage.image = UIImage(named: "profileIcon")
    }

    return cell
}