From Google Firestore, I am fetching modeled user data individually. Once 1 user's data has been completely fetched, a completion handler is executed and the user will be added to a global array of users (self.activeUsers) and then I call reloadData() on my UITableView (self.activeUsersTableView). I noticed users are getting loaded on top of each other and it is not a UI construction issue or auto layout issues. I can tell because when I print out the indexPath.row in cellForRowAt, I notice duplicate prints of the same index. Anybody got an idea as to why? reloadData() is ONLY called in 1 place for this UITableView which is in the body of myCompletionHandler
private lazy var activeUsersTableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
table.dataSource = self
table.delegate = self
table.allowsSelection = false
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
loadActiveUsersTableView()
fetchActiveUsers()
}
func loadActiveUsersTableView() {
self.view.addSubview(activeUsersTableView)
self.activeUsersTableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(
[
self.activeUsersTableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150),
self.activeUsersTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 30),
self.activeUsersTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -280),
self.activeUsersTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -250),
]
)
}
func fetchActiveUsers() {
let myCompletionHandler: (UserProfile) -> Void = { theUser in
self.activeUsers.append(theUser)
self.activeUsersTableView.reloadData()
}
self.chatsDocRef!.getDocument { document, error in
if let error = error as NSError? {
print("Error getting document: \(error.localizedDescription)")
}
else {
if let document = document {
let data = document.data()
self.activeUserUIDS = data?["users"] as? [String]
for i in 0..<(self.activeUserUIDS?.count ?? 0) {
print("Retrieving document for user ID: ", self.activeUserUIDS![i])
let userDocRef = Firestore.firestore().collection("users").document(self.activeUserUIDS![i])
UserProfile.getUserProfileData(userDocRef, using: myCompletionHandler)
}
}
}
}
}
Here is the cellForRowAt call and followed by that is my output from the print method. I left out the code AFTER the print for better readability (it isn't important in this case)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("The row", indexPath.row)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
OUTPUT: The row 0 The row 0 The row 1 The row 0 The row 1 The row 2 The row 0 The row 1 The row 2 The row 3 The row 0 The row 1 The row 2 The row 3 The row 4 The row 0 The row 1 The row 2 The row 3 The row 4 The row 5 The row 0 The row 1 The row 2 The row 3 The row 4 The row 5 The row 0 The row 1 The row 2 The row 3 The row 4 The row 5 The row 0 The row 1 The row 2 The row 3 The row 4 The row 5
CodePudding user response:
the real root of the problem was the fact I was not using custom cell design and clearing my cells prior to reuse by using prepareForReuse() method. I implemented a custom cell design and this fixed my issue.