【问题标题】:How to refresh TableViewCell after deleting data in firebase?删除firebase中的数据后如何刷新TableViewCell?
【发布时间】:2018-12-20 16:07:24
【问题描述】:

我想在通过滑动操作表删除对话后刷新我的对话(表格视图单元格)。

我尝试在删除数据后重新加载表格视图,但它不起作用。还有异步。

 // Swipe Action Sheet solution
func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{

    // Write action code for the Flag
    let deleteAction = UIContextualAction(style: .normal, title:  "Löschen", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        let ref = Database.database().reference()
        ref.child("users").child((self.currentUser?.uid)!).child("conversations").child((self.items[indexPath.row].user.uid)!).removeValue()
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
        success(true)
    })
    deleteAction.backgroundColor = .red
    return UISwipeActionsConfiguration(actions: [deleteAction])

}

提前感谢您的帮助!

【问题讨论】:

    标签: swift firebase


    【解决方案1】:

    如果没有任何错误,您必须在removeValue 方法的完成块中重新加载数据。此外,在重新加载数据之前,您必须从 items 数组中删除项目

    .removeValue { error,_ in
        if error == nil {
            self.items.remove(at: indexPath.row)
            self.tableView.reloadData() // self.tableView.deleteRows(at: [indexPath], with: .automatic)
        }
        success(error == nil)
    }
    

    【讨论】:

    【解决方案2】:

    要使用滑动删除行,您可以使用

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == .delete {
                items.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: .fade)
            } 
        }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    
        return true
    }
    

    而且因为您没有重新加载表格视图,所以看起来会更好

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 2017-08-14
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多