【发布时间】:2020-05-27 06:13:18
【问题描述】:
我在我的应用程序上创建了一个表格视图,我想添加可以让用户删除表格视图中的行的选项。 我正在使用这个功能 h1 是我把它放在tableview 中的数组字符串。 我的错误是当我尝试删除 func editinstyle 中的行时
var h1:[String] = ["one","two","three"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
h1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath as IndexPath) as! TableView1
cell.information.text = h1[indexPath.row]
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
h1.remove(at: indexPath.row) //Remove element from your array
print(h1)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
当我尝试单击删除它时显示如下2020-02-11 20:43:35.803024+0200 TraniersApp[13314:401189] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新: 第 0 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于更新 (2) 之前该节中包含的行数,加上或减去行数从该部分插入或删除(0 插入,1 删除),加上或减去移入或移出该部分的行数(0 移入,0 移出)。'
有人知道问题出在哪里吗?
【问题讨论】:
-
您能否在调用
remove(at:)之前打印h1以查看remove(at:)是否实际执行任何操作? -
您显示的代码是正确的,但我怀疑这不是您的实际代码,因为屏幕截图中的输出不同。异常表明您的
numberOfRows函数存在问题。似乎还涉及另一个数组value -
@Paulw11 谢谢,它有效
标签: ios swift iphone xcode uitableview