Home TableView row needs to de deleted twice before isn't removes form the UI?
Reply: 1

TableView row needs to de deleted twice before isn't removes form the UI?

Carter Cobb
1#
Carter Cobb Published in 2018-01-13 04:35:16Z

I have got to the point where I can add cells that all have different labels. But, when the row is swiped to be deleted it will only delete the text associated with the cell and not the cell itself. I've tried to add SavedMessages.deleteRows(at: [indexPath], with: .automatic) inside the if statement but every time the app tries to delete the row, the app crashes.

Here is the code:-

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
   if editingStyle == UITableViewCellEditingStyle.delete {
      let delete: NSFetchRequest<Messages> = Messages.fetchRequest()
      do { 
          var deleteMessage = try
          PersistenceServce.context.fetch(delete)
          PersistenceServce.context.delete(messages.remove(at: indexPath.row))
          PersistenceServce.saveContext()
          self.messages = deleteMessage
          SavedMessages.reloadData()
      } catch {}
   }
}

Thanks in advance!

Ashish Sharma
2#
Ashish Sharma Reply to 2018-01-13 06:31:22Z

Maybe you are not using

tableView.cellForRow(at: indexPath)?.removeFromSuperview()

I created an example where removing data as well as the cell is getting removed perfectly fine.

var data = [1,2,3,4,5]

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

    override func tableView(_ tableView: UITableView, 
    commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete
    {

        data.remove(at: indexPath.row) //removing the value from data source.

        tableView.cellForRow(at: indexPath)?.removeFromSuperview() //removing the cell

        tableView.reloadData() //reloading Table data.
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.33703 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO