Iphone 删除数组中的项目,在Swift中的另一个数组中
我有一个数组,它是表视图中的一个节数组。 我有另一个数组,其中包含数组。 我在表视图中正确地显示了它,但每次我想删除它时,都会出现一个错误。 我想删除数组中位于数组内的项目。 以下是我到目前为止得到的信息:Iphone 删除数组中的项目,在Swift中的另一个数组中,iphone,arrays,swift,Iphone,Arrays,Swift,我有一个数组,它是表视图中的一个节数组。 我有另一个数组,其中包含数组。 我在表视图中正确地显示了它,但每次我想删除它时,都会出现一个错误。 我想删除数组中位于数组内的项目。 以下是我到目前为止得到的信息: class ViewController: UIViewController, UITableViewDelegate { @IBOutlet var tableView: UITableView! var eachSection = ["a
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var eachSection = ["a","ss","dd","heyyyyyy","3"]
var eachStep = [["z","zz"],["x","xx"],["c","cc","ccc"],["r","rr","rrr","rrrr"],["o"]]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return eachSection.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionString = Array(eachStep)[section]
return sectionString.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
let sectionString = Array(eachStep)[indexPath.section]
cell.textLabel?.text = sectionString[indexPath.row]
print(sectionString)
return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionString = eachSection[section]
return sectionString
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if (editingStyle == UITableViewCellEditingStyle.Delete) {
var sectionString = Array(eachStep)[indexPath.section]
sectionString.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}
这是我得到的错误:
由于未捕获异常而终止应用程序
'NSInternalInconsistencyException',原因:'无效更新:无效
节0中的行数。表中包含的行数
更新(2)后的现有节数必须等于
更新(2)之前该节中包含的行,加号或减号
从该节插入或删除的行数(插入0,
1)并加上或减去移入或移出的行数
该部分(0移入,0移出)
此错误包含您问题的答案 由于未捕获异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 节0中的行数。表中包含的行数 更新(2)后的现有节数必须等于 更新(2)之前该节中包含的行,加号或减号 从该节插入或删除的行数(插入0, 1)并加上或减去移入或移出的行数 该部分(0移入,0移出) 仔细看看你在干什么 从一个数组中删除不用作数据源的项
var sectionString = Array(eachStep)[indexPath.section]
sectionString.removeAtIndex(indexPath.row)
试试这个:
eachStep[indexPath.section].removeAtIndex(indexPath.row)
你的数组是可变的吗?数组中的数据是否来自WebService响应(json格式的数据)?@OleggordiChuk由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的行数无效。”。更新(2)后现有节中包含的行数必须等于更新(2)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出)你好请不要在评论中发布错误消息或代码(不可读,评论可以随时删除)。你应该回答你的问题。非常感谢。是的,这是错误,但我如何纠正它,为什么?我现在理解了我的错误,但我不知道如何纠正它
sectionString
是每个步骤中数组的副本。您需要执行类似于eachStep[indexPath.section]=sectionString的操作才能将其复制回来。您的数据源始终已满,因为您从数据源副本中删除了项。