带有部分的Swift表视图显示单元格中的冗余数据



我有一个包含部分的表视图。不幸的是,如果我的数组中有多个项目,我的表视图会显示多余的单元格。如果我打印数组的大小,那么一切都是正确的。我认为这与我的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

}

尝试将其添加到UITableViewDataSource

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

相关内容

最新更新