gpt4 book ai didi

ios - UILabel 文本编辑后 UITableViewCell 重复

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

我有一个 UITabelViewCell 用于显示与联系人的事件聊天。

有一个标签显示每个联系人的未读消息数量,信息来自 Firebase。

当聊天打开时,显示未读消息数量的 UILabel 应该消失。不过,一旦完成,它只会创建另一个单元格,如下图所示:

Before opening chat

然后我单击聊天,聊天打开,然后单击“后退”导航项,这显示:

After navigating back to TableView

这是我的课:

import UIKit
import Firebase

class MessagesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{

private var chats = [PrivateChatLiteObject]()

@IBOutlet weak var ChatsTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()

ChatsTableView.dataSource = self
ChatsTableView.delegate = self
self.view.layer.backgroundColor = UIColor.white.cgColor
populateActiveChats()
}

private func populateActiveChats()
{
let loggedOnUserID = Auth.auth().currentUser?.uid
let ref = Constants.refs.databaseChatsLite.child(loggedOnUserID!)

// Retrieve the products and listen for changes
ref.observe(.value, with:
{ (snapshot) in

for child in snapshot.children.allObjects as! [DataSnapshot]
{
// Code to execute when new product is added

let chatValueDictionary = child.value as? NSDictionary
self.AddChatToCollections(chatAsDictionary: chatValueDictionary, contactID: child.key)
self.DispatchQueueFunc()

}
})
}

func AddChatToCollections(chatAsDictionary: NSDictionary!, contactID: String)
{
let contactName = chatAsDictionary["userName"] as! String
//let contactImg = chatAsDictionary["userImageStr"] as? String
//let lastMsg = chatAsDictionary["lastMessage"] as! String
let newMsgs = chatAsDictionary["newMessage"] as! Int


let chatToAdd = PrivateChatLiteObject(chattingWith: contactName, ContactID: contactID, unreadMessages: newMsgs, LastMSG: "")

chats.append(chatToAdd)
}

func DispatchQueueFunc()
{
DispatchQueue.main.async
{
self.ChatsTableView.reloadData()
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return chats.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = ChatsTableView.dequeueReusableCell(withIdentifier: "chat_room_cell", for: indexPath) as! PrivateChatUITableViewCell

//cell.ContactImageView = UIImageView.loadImageUsingUrlString(contactImg)
let index = indexPath.row
cell.ContactName.text = chats[index].GetContactName()

cell.ContactID = chats[index].GetContactID()

let unreadMSGs = chats[index].GetUnreadMessagesNumber()

if unreadMSGs == 0
{
cell.NewNotificationsNum.isHidden = true
}
else
{
cell.NewNotificationsNum.isHidden = false
let stringNotifications = String(unreadMSGs)
cell.NewNotificationsNum.text = stringNotifications
}

return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

chatIndexToLoad = indexPath.row
performSegue(withIdentifier: "segue_to_chatroom", sender: self)
}

var chatIndexToLoad: Int = -1

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "segue_to_chatroom")
{
let destination = segue.destination as! PrivateChatViewController
destination.senderId = Constants.refs.currentUserInformation!.uid
//destination.senderDisplayName = Constants.refs.currentUserInformation?.displayName
destination.contactDisplayName = chats[chatIndexToLoad].GetContactName()
destination.contactID = chats[chatIndexToLoad].GetContactID()

}
}

}

我猜它必须在节点内部发生变化时从 Firebase 读取数据,并重新添加相同的节点。我需要一种方法,仅在添加新节点时才真正启动 Firebase 的读取。我尝试使用 .childAdded 但这不起作用。有谁知道如何解决这个问题?

我唯一能想到的是每当向集合中添加项目时,检查具有相同联系人 ID 的聊天是否尚不存在,但这对性能不利。

最佳答案

我不太清楚您的要求,但如果您愿意,我可以在下面分享我当前的代码,该代码可以工作。此外,请确保将 UITableView 作为委托(delegate)和数据源附加到 ViewController 并将其添加到您的 View Controller 类中:

class NearbyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
var handle:DatabaseHandle?
var ref:DatabaseReference?






func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}

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

let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]
cell.textLabel?.text = cellName

return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]

print("row\(indexPath.row)")
print("name: \(cellName)")


}


override func viewDidLoad() {
super.viewDidLoad()






ref = Database.database().reference()
handle = ref?.child("list").observe(.childAdded, with: { (snapshot) in
if let item = snapshot.value as? String
{
self.myList.append(item)
self.nearbyTableView.reloadData()
}
})
}
}

关于ios - UILabel 文本编辑后 UITableViewCell 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669056/

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