当我从 tableview Swift 中删除一行时出现错误
Bug When I delete a row from tableview Swift
我正在尝试使用 editActionForRowAt
从 table 视图中删除行,但是当我尝试删除时,元素已从数据库中删除,但应用程序崩溃并出现此错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
这是我的视图控制器代码:
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]
}
}
您还必须从本地数组中删除数据。
用下面的方法替换你的方法
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]
}
您还必须从数据源数组中删除该项,在这种情况下 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)
}
我正在尝试使用 editActionForRowAt
从 table 视图中删除行,但是当我尝试删除时,元素已从数据库中删除,但应用程序崩溃并出现此错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
这是我的视图控制器代码:
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]
}
}
您还必须从本地数组中删除数据。
用下面的方法替换你的方法
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]
}
您还必须从数据源数组中删除该项,在这种情况下 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)
}