我正在处理一个无法解决的问题......我有一个来自客户数据库数组的名称表,每个客户在其他数据成员中都有一个名称属性。
我可以成功删除某个部分中的行,但我不能删除该部分(当该部分中的最后一行被删除时,该部分必须消失)。
我得到了:
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).
我知道该表在数据的幕后进行了一些健全性检查,这应该匹配,但我无法确定调用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
在 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()
}
}
}
在没有看到代码的其余部分的情况下,这应该可以工作,但是在执行消失部分时,tableViews可能会很棘手。
关于ios - 在 Swift 中使用 commitEditingStyle 动态删除 UITable 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29748089/