Search code examples
iosswifttableviewcelltemporary

How can I add a temporary cell with new data?


I am trying to add a temporary cell with data the user has just inputted.

With the code bellow I expected to do that, but instead what I found is that a duplicate of the last cell is added in its spot, how can I fix this to add the current data?

         self.tableView.beginUpdates()
        self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)//self.arrayOfComments.count-1
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "commentCell", for: IndexPath(row: 0, section: 0)) as! commentTableViewCell
        self.addTemporaryComment(cell: cell)
        self.tableView.endUpdates()

Solution

  • This

      let cell = self.tableView.dequeueReusableCell(withIdentifier: "commentCell", for: IndexPath(row: 0, section: 0)) as! commentTableViewCell
    

    will return a random non-visible cell with it's content , what you need is to create a new object from your model and append it to the datasource array of the table


    arr.insert(yourObject,at:0)