是否需要两次删除TableView行才能从UI中删除? [英] TableView row needs to de deleted twice before isn't removes form the UI?

查看:71
本文介绍了是否需要两次删除TableView行才能从UI中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以添加所有带有不同标签的单元格了。但是,当将行扫过以删除时,它将仅删除与单元格关联的文本,而不是单元格本身。我试图在 if 内添加 SavedMessages.deleteRows(at:[indexPath],with:.automatic)语句,但是每次应用尝试删除该行时,应用都会崩溃。

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.

这是代码:-

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

谢谢!

推荐答案

也许您未使用

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

这篇关于是否需要两次删除TableView行才能从UI中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆