我正在处理一个无法解决的问题......我有一个来自客户数据库数组的名称表,每个客户在其他数据成员中都有一个名称属性。
我可以成功删除某个部分中的行,但我不能删除该部分(当该部分中的最后一行被删除时,该部分必须消失)。
I got:
'NSInternalInconsistencyException',原因:'无效更新:无效
节数。表中包含的节数
更新后的视图(3)必须等于节数
更新前的表视图中包含的内容 (4),加上或减去
插入或删除的节数(0 插入,0 删除)。
我知道该表在数据的幕后进行了一些健全性检查,这应该匹配,但我无法确定在调用 deleteRowsAtIndexPaths 之前的确切时间?后?我应该什么时候更新我的属性和/或字典?我应该管理 numberOfSectionsInTableView 数据源方法吗?
我重复一遍,对于删除的行,它工作正常,表移出行并正确更新。该部分的最后一行是交易......
我想我错过了一些东西,这就是我问的原因……也找不到任何帮助阅读。
非常感谢大家!
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from the array and updating the tableview)
//Check if delete was press
if editingStyle == .Delete {
//Delete row from dataSource
if let tv = tableView
{
customerList.removeAtIndex(returnPositionForThisIndexPath(indexPath, insideThisTable: tableView))
// Deletes the name of the customer from the customer list array, sorted by name
fillArrayOfNames()
//Fill the array of names for the sections-table, creating a dictionary with the name initials
//updated from the customer list array (below)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) //Crash in this line
tableView.reloadData()
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return dictionaryOfPatientsInitials.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var keysFromDictionary = dictionaryOfPatientsInitials.keys.array
keysFromDictionary.sort(<)
let keyByOrder = keysFromDictionary[section]
let arrayInThisSection = dictionaryOfPatientsInitials[keyByOrder]
return arrayInThisSection!.count
}
你已经快到了,但你需要一种方法来检测某个部分是否已经消失,以及哪个部分已经消失,此时你可以调用deleteSections
将更新部分括在 a 中beginUpdate
/ endUpdate
打电话但不打电话reloadData
(有关这些方法,请参阅文档)
/**
remove customer from model layer
:param: index index of customer to remove
:returns: return section that was removed or nil if none was
*/
func removeCustomer(index:Int)->Int? {
var removedSectionOrNil:Int? = nil
//logic to remove customer, rebuild model and detect if section has gone also
return removedSectionOrNil
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from the array and updating the tableview)
//Check if delete was press
if editingStyle == .Delete {
//Delete row from dataSource
if let tv = tableView
{
tv.beginUpdates()
let position = returnPositionForThisIndexPath(indexPath, insideThisTable: tableView)
let removedSection = removeCustomer(position)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) //Crash in this line
if let removedSection = removedSection {
tv.deleteSections(sections:NSIndexSet(index: removedSection) as IndexSet, withRowAnimation: .Automatic)
}
tv.endUpdates()
}
}
}
在没有看到其余代码的情况下,这should工作,但在执行消失部分时,表视图可能会很棘手。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)