tableView.reloadData() 添加新单元格而不是重新加载数据

tableView.reloadData() add new cell instead of reload the data

我尝试调用“reloadData”,但它没有重新加载数据,而是添加了一个新单元格 像这样。这是一个非常烦人的错误。每次我回到这个页面,单元格每次都在增加,我不知道为什么。

下图是我遇到的一个bug。

这是我的代码:

 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
}
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.

=========================================== ===========================