带部分的Swift Tableview在单元格中显示了冗余数据

时间:2019-11-02 16:16:49

标签: swift uitableview

我有一个带有部分的表格视图。不幸的是,如果我的数组中有多个项目,则表视图将显示冗余单元格。如果我要打印数组的大小,那么一切都正确。我认为这与我的cellForRowAt方法有关,但我找不到问题。

 func getContacts(){
    let db = Firestore.firestore()
    let docRef = db.collection("Users").document(Auth.auth().currentUser!.uid)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")

            self.userids = document.get("contacts") as! [String]
                     if self.userids.count > 0{
                         self.getUserGroupsDetails(path: "Users", userids: self.userids)
                     }else{
                         self.headerTitles.remove(at: 2)
                         self.collectionView.reloadData()
                     }
        } else {
            print("Document does not exist")
        }
    }
}

func getMyGroups() {
    let db = Firestore.firestore()
    let docRef = db.collection("Users").document(Auth.auth().currentUser!.uid)

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"

          self.groupids = document.get("myGroups") as! [String]
          if self.groupids.count>0{
              self.getUserGroupsDetails(path: "Groups",  userids: self.groupids)

          }else{
              self.headerTitles.remove(at: 1)
              self.collectionView.reloadData()
          }
        } else {
            print("Document does not exist")
        }
    }

}

func getUserGroupsDetails(path: String, userids: [String]){
    for userid in userids{
         let db = Firestore.firestore()
         db.collection(path).document(userid)
               .addSnapshotListener { documentSnapshot, error in

                 guard let document = documentSnapshot else {
                   print("Error fetching document: \(error!)")
                   return
                 }
                 guard let data = document.data() else {
                    print("Document data was empty.")
                   return
                 }
                let contactObject = ContactObject.init()
                contactObject.id = document.documentID
                contactObject.name = data["name"] as! String
                contactObject.imageUrl = data["imageUrl"] as! String
                print("called", path)
                if (path == "Groups"){
                contactObject.isGroup = true
                self.myGroups.append(contactObject)
                self.data.append(self.myGroups)
                self.collectionView.reloadData()
                }else{
                contactObject.isGroup = false
                self.myUsers.append(contactObject)
                self.data.append(self.myUsers)
                self.collectionView.reloadData()
            }

                print("Current data: \(data)")
        }

    }


    self.collectionView.reloadData()
}

这是我的tableView方法:

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return data[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if data[indexPath.section][indexPath.row].name == "Freunde" || data[indexPath.section][indexPath.row].name == "Interessen (öffentlich)"{
       var cell1 = tableView.dequeueReusableCell(withIdentifier: "selectStory", for: indexPath) as! StorySelectionTableViewCell

        cell1.label.text = data[indexPath.section][indexPath.row].name
        cell1.select.setOn(false, animated: true)
        cell1.select.tag = indexPath.row
        cell1.select.addTarget(self, action: #selector(handleSwitch), for: .valueChanged)
        return cell1
    }

        var cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactsTableViewCell
        cell.select.tag = indexPath.row
        cell.thumb.layer.masksToBounds = false
        cell.thumb.layer.cornerRadius = cell.thumb.frame.height/2
        cell.thumb.clipsToBounds = true
        cell.name.text =  data[indexPath.section][indexPath.row].name
        cell.select.addTarget(self, action: #selector(handleButtonPress), for: .touchDown)
        if data[indexPath.section][indexPath.row].imageUrl != "" && data[indexPath.section][indexPath.row].imageUrl != nil{

            let url = URL(string:  data[indexPath.section][indexPath.row].imageUrl!)
            cell.thumb.kf.setImage(with: url)

            }
        return cell



}

1 个答案:

答案 0 :(得分:0)

尝试将其添加到您的UITableViewDataSource

func numberOfSections(in tableView: UITableView) -> Int {
    return data.count
}