Arrays EditingStyle-删除数组中的单元格和项
我已经创建了一个数组Arrays EditingStyle-删除数组中的单元格和项,arrays,swift,uitableview,editing,Arrays,Swift,Uitableview,Editing,我已经创建了一个数组 var subjectsData=[科目(名称:“投资”,学期:1),科目(名称:“统计”,学期:1),科目(名称:“大学研究”,学期:2)] 还有一张桌子 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCe
var subjectsData=[科目(名称:“投资”,学期:1),科目(名称:“统计”,学期:1),科目(名称:“大学研究”,学期:2)]
还有一张桌子
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SubjectCell", forIndexPath: indexPath) as! SubjectCell
let subject = subjects[indexPath.row] as Subject
let Semester = "\(subject.semester)"
cell.nameLabel.text = subject.name
cell.semesterLabel.text = "Semester"
cell.semesterNumberLabel.text = Semester
return cell
}
使用var subjects:[Subject]=subjectsData
我现在尝试创建一个编辑函数,以便删除单元格/行,从而删除数组中的相应项
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
subjectsData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
在我看来,代码看起来逻辑性和功能性都很好,但每当我试图删除行/单元格并收到错误时,运行应用程序时就会崩溃:
'NSInternalInconsistencyException',原因:'无效更新:节0中的行数无效。更新(3)后现有节中包含的行数必须等于更新(3)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出)“
每当我删除行/单元格时,数组中相应的项也应该删除,但是如果我正确理解错误,数组似乎不会更新/删除该项
如果有人知道为什么会发生这种错误以及如何解决它,我将不胜感激 好的,所以我将
subjectsData.removeAtIndex(indexPath.row)
更改为这个编辑样式代码
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
subjects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
因为我说了var subjects:[Subject]=subjectsData
我不知道为什么会这样处理错误,但现在当我尝试删除某些内容时,应用程序不再崩溃。好的,所以我将
subjectsData.removeatinex(indexPath.row)
更改为此编辑样式代码
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
subjects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
因为我说了var subjects:[Subject]=subjectsData
我不知道为什么会出现这样的错误,但现在当我尝试删除某些内容时,应用程序不再崩溃