我嘗試呼叫“reloadData”,但不是重新加載資料,而是添加一個像這樣的新單元格。這是一個非常煩人的錯誤。每次我回到這個頁面,單元格每次都在增加,我不知道為什么。
下圖是我遇到的一個錯誤。


這是我的代碼:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "YourSessionViewCell", bundle: nil), forCellReuseIdentifier: "yourSessionViewCellIden")
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(getSessionData), userInfo: nil, repeats: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
func getSessionData(){
self.yourSessionModel = []
Firestore.firestore().collection("MSession").getDocuments(completion: { [self]
snapshot, error in
if error != nil {
print("error in calen firebase")
} else {
if !snapshot!.isEmpty {
let ary = snapshot!.documents
for item in ary {
let eventMSession = YourSessionModel(dict: item.data())
if(eventMSession.createdBy == AuthUser.userId()){
self.yourSessionModel.append(eventMSession)
self.users.append(YourSessionStruct(sName: eventMSession.roomName ?? "", sDes: eventMSession.subject ?? "", sCreateAt: eventMSession.createdAt ?? ""))
}
}
}
}
self.tableView.reloadData()
})
}
//UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
if(users.count == 0){
return 0
}
return users.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourSessionViewCellIden") as! YourSessionViewCell
cell.sessionName.text =
"\(users[indexPath.section].sName)"
cell.SessionDes.text =
" \(users[indexPath.section].sDes)"
cell.SessionCreateAt.text =
" \(users[indexPath.section].sCreateAt)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 220
}
uj5u.com熱心網友回復:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "YourSessionViewCell", bundle: nil), forCellReuseIdentifier: "yourSessionViewCellIden")
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(getSessionData), userInfo: nil, repeats: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
func getSessionData(){
self.yourSessionModel = []
self.users = [YourSessionStruct]() // Add this below self.yourSessionModel
Firestore.firestore().collection("MSession").getDocuments(completion: { [self]
snapshot, error in
if error != nil {
print("error in calen firebase")
} else {
if !snapshot!.isEmpty {
let ary = snapshot!.documents
for item in ary {
let eventMSession = YourSessionModel(dict: item.data())
if(eventMSession.createdBy == AuthUser.userId()){
self.yourSessionModel.append(eventMSession)
self.users.append(YourSessionStruct(sName: eventMSession.roomName ?? "", sDes: eventMSession.subject ?? "", sCreateAt: eventMSession.createdAt ?? ""))
}
}
}
}
self.tableView.reloadData()
})
}
//UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
if(users.count == 0){
return 0
}
return users.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourSessionViewCellIden") as! YourSessionViewCell
cell.sessionName.text =
"\(users[indexPath.section].sName)"
cell.SessionDes.text =
" \(users[indexPath.section].sDes)"
cell.SessionCreateAt.text =
" \(users[indexPath.section].sCreateAt)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 220
}
在 self.yourSessionModel 下面添加 self.users = YourSessionStruct 應該可以解決問題。
==================================================== =======================
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417686.html
標籤:
