我正在尝试使用editActionForRowAt
从表视图中删除行,但是当我尝试删除该元素时,该元素已从数据库中删除,但应用程序崩溃了,并且出现此错误:
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第0节中的行数无效。更新(2)之后现有节中包含的行数必须等于行数更新(2)之前包含在该部分中的内容,加上或减去从该部分中插入或删除的行数(已插入0,已删除1),以及加上或减去移入或移出该部分的行数(移入0) ,0移出)。'
这是我的视图控制器代码:
class ShowTargetVC: UIViewController , UITableViewDataSource,UITableViewDelegate{
let Images = ["airplane","ambulance","analytics","backpack","ball","book","birthday-cake","brainstorm","business-partnership","car","coffee","commission","contract","drama","emergency","food","friends","grandparents","growth","home","hotel","newlyweds","sexual-harassment","taxi","workspace"]
@IBOutlet weak var tableView: UITableView!
var transactions = [Transaction]()
override func viewDidLoad() {
super.viewDidLoad()
API.getTransaction(username: "1",type : "target") { (error :Error?, transactions : [Transaction]?) in
if let transactions = transactions {
self.transactions = transactions
self.tableView.reloadData()
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return transactions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "target")
let contentView = cell?.viewWithTag(1)
let image = contentView?.viewWithTag(2) as! UIImageView
let name = cell!.viewWithTag(3) as! UILabel
let money = cell!.viewWithTag(4) as! UILabel
image.image = UIImage (named: Images[indexPath.item])
name.text = transactions[indexPath.item].name
//print(transactions[indexPath.item].name)
money.text = String(transactions[indexPath.item].trMoney)
return cell!
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deletAction = UITableViewRowAction(style: .default, title: "Delete") { (UITableViewRowAction
, IndexPath) in
API.deleteTransaction(id: String(self.transactions[indexPath.item].id))
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [IndexPath], with: .automatic)
self.tableView.endUpdates()
}
return [deletAction]
}
}
答案 0 :(得分:0)
您还必须从本地阵列中删除数据。
用下面的一个替换您的方法
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deletAction = UITableViewRowAction(style: .default, title: "Delete") { (UITableViewRowAction
, IndexPath) in
API.deleteTransaction(id: String(self.transactions[indexPath.item].id))
self.tableView.beginUpdates()
// ** add below line. **
self.transactions.remove(at: IndexPath.row)
self.tableView.deleteRows(at: [IndexPath], with: .automatic)
self.tableView.endUpdates()
}
return [deletAction]
}
答案 1 :(得分:0)
您还必须从数据源数组中删除该项目,在这种情况下,beginUpdates / endUpdates
毫无意义。
强烈建议在动作闭包lowercased
中命名参数。
变量IndexPath
可能与结构IndexPath
冲突。
let deletAction = UITableViewRowAction(style: .default, title: "Delete") { (_, indexPath) in
API.deleteTransaction(id: String(self.transactions[indexPath.row].id))
self.transactions.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}