目前,我正在加载来自firebase的帖子。当我加载它们并将它们添加到数组时,我命令数组使得帖子将按时间戳排序。订购帖子时,配置文件图像有时会显示不正确的单元格。在我将单元格出列无效之前,我已尝试设置image = nil。我认为问题在于排序,因为在我订购数组时配置文件图像只显示不正确。当我不对阵列进行排序时,图像会正确显示。
获取单元格信息的代码:
func fetchMessages() {
let userID = Auth.auth().currentUser?.uid
let messageRef = Database.database().reference().child("convoMembers").child(userID!)
messageRef.observe(.value, with: { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot]{
let conversationKey = child.key
let conversationStarted = child.value as? String
let conversationReference = Database.database().reference().child("conversations").child(conversationKey)
conversationReference.observeSingleEvent(of: .value, with: { (snapshot) in
if let value = snapshot.value as? NSDictionary {
let convo = Convo()
convo.lastMessage = value["last_message"] as? String ?? "Last message not found"
convo.time = value["created_at"] as? String ?? "Created at not found"
convo.timeOfDeletion = value["revealedDate"] as? Int
convo.timeOfLastMessage = value["last_message_time"] as? Int ?? 0
convo.convoID = snapshot.key
convo.convoStarted = conversationStarted
self.convoList.append(convo)
self.convoList.sort() { $0.timeOfLastMessage! > $1.timeOfLastMessage! }
self.tableView.reloadData()
}
}, withCancel: { (error) in
print (error)
})
if self.queryComplete == true {
conversationReference.observe(.value, with: { (snapshot) in
let changedKey = snapshot.key
if let value = snapshot.value as? NSDictionary {
if let index = self.convoList.index(where: {$0.convoID == changedKey}) {
// let indexPath = IndexPath(row: index, section: 0)
let changedConvo = self.convoList[index]
changedConvo.lastMessage = value["last_message"] as? String ?? "Last message not found"
changedConvo.timeOfLastMessage = value["last_message_time"] as? Int
if snapshot.hasChild("information") {
changedConvo.revealedBool = true
if let information = snapshot.childSnapshot(forPath: "information").childSnapshot(forPath: conversationStarted! ).value as? NSDictionary {
changedConvo.name = information["Name"] as? String
changedConvo.profileImage = information["profileImage"] as? String
/*
let nameReference = Database.database().reference().child("users").child(changedConvo.otherUserID!)
nameReference.observeSingleEvent(of: .value, with: { (snapshot) in
if let value = snapshot.value as? NSDictionary {
changedConvo.name = value["Name"] as? String ?? "no name found"
changedConvo.profileImage = value["profileImage"] as? String ?? "nil"
print(changedConvo.name!)
}
})
*/
}
}
self.convoList.sort() { $0.timeOfLastMessage! > $1.timeOfLastMessage! }
DispatchQueue.main.async { self.tableView.reloadData() }
}
}
})
}
}
self.queryComplete = true
}) { (err) in
print (err)
}
}
和加载单元格的功能:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if queryComplete == true {
if displayConvo == true {
let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath) as! messageTableViewCell
cell.profileImage.image = nil
let convoContent = convoList[indexPath.row]
cell.lastMessage.text = convoContent.lastMessage
if convoContent.revealedBool == true {
cell.name.text = convoContent.name
let doesNotHavePic = (convoContent.profileImage == "nil")
if doesNotHavePic {
cell.profileImage.image = #imageLiteral(resourceName: "images.png")
}
else if convoContent.profileImage == nil {
cell.profileImage.image = #imageLiteral(resourceName: "images.png")
}
else {
let url = URL(string: convoContent.profileImage!)
let processor = RoundCornerImageProcessor(cornerRadius: cell.profileImage.frame.size.width / 2)
cell.profileImage.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
}
}
else {
let currentTime = UInt64(Date().timeIntervalSince1970 * 1000)
let difference = convoContent.timeOfDeletion! - Int(currentTime)
let date = Date(timeIntervalSince1970: TimeInterval(difference / 1000))
cell.name.text = String(timeToDelete(date: date as NSDate, numericDates: false))
}
let lastMessageDate = Date(timeIntervalSince1970: TimeInterval(convoContent.timeOfLastMessage! / 1000))
cell.timeOfLastMessage.text = String(timeAgo(date: lastMessageDate as NSDate, numericDates: false))
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath) as! messageTableViewCell
cell.lastMessage.text = "No new messages"
return cell
}
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath as IndexPath) as! messageTableViewCell
print ("loading")
return cell
}
}