gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:33:05 25 4
gpt4 key购买 nike

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

关于带部分的 Swift 表格 View 显示单元格中的冗余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com