【发布时间】:2017-08-15 08:10:00
【问题描述】:
我在 storyboard 中创建了 Tableview。当我尝试删除tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction] 中的单元格时,表在删除后不会立即重新加载。它也将在数据库中成功删除。
当我关闭视图并返回时,它将被更新。我哪里错了?
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .normal, title: "Löschen") { (rowAction, indexPath) in
let Info: NSDictionary = self.finaltaskValue[indexPath.item] as! NSDictionary
let pwid = Info["pwid"] as! String
self.finaltaskValue.remove(at: indexPath.row)
self.finalTasktableView.beginUpdates()
self.finalTasktableView.deleteRows(at: [indexPath], with: .none)
self.finalTasktableView.endUpdates()
let url = URL(string: "...php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let body = "parameters"
request.httpBody = body.data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
if error == nil {
DispatchQueue.main.async(execute: {
do{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
guard let parseJSON = json else {
print("")
return
}
DispatchQueue.main.async(execute: {
self.finalTasktableView.reloadData()
})
}catch {
//print("Caught an error: \(error)")
DispatchQueue.main.async(execute: {
let message = "Server ist Offline! Wir arbeiten mit hochdruck dran alles zu reparieren"
}
})
}else {
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
}
}.resume()
}
deleteAction.backgroundColor = colorDarkGreen
return [deleteAction]
}
【问题讨论】:
-
使用
deleteRows(at后不要重新加载表格视图。此方法确实会更新 UI。顺便说一句,删除begin-/endUpdates()。两条线都没有效果。将dataTask分派到main 线程的目的是什么?dataTask无论如何都使用自己的线程。
标签: ios swift uitableview