tableView.reloadData() add new cell instead of reload the data
Solution 1:
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
}
Adding self.users = YourSessionStruct below self.yourSessionModel should solve the problem.
========================================================================