ios - 在 Swift 中使用 commitEditingStyle 动态删除 UITable 部分

标签 ios objective-c xcode uitableview swift

我正在处理一个无法解决的问题......我有一个来自客户数据库数组的名称表,每个客户在其他数据成员中都有一个名称属性。

我可以成功删除某个部分中的行,但我不能删除该部分(当该部分中的最后一行被删除时,该部分必须消失)。

我得到了:

'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/

相关文章:

iphone - 将所有对象属性放入 NSDictionary 或 NSArray

ios - 在 iOS 中显示 View 之前显示提示警报对话框

ios - 在 Xcode 中,即使图标位于 asset 文件夹中,也看不到我的 info.plist 文件。解决办法是什么?

ios - 如何在我的 iPhone 应用程序中实现来自特定用户的 Instagram 照片/提要?

ios - 使用动态高度和全高创建不同的单元尺寸

iphone - 与多个开发人员一起处理同一个 iOS 项目时的 Xcode 4 工作流程

Swift,字典解析错误

ios - OCUnit 已弃用。将 Xcode 升级到 5.1 后出错

javascript - 我的图像在 Chrome 和 Firefox 中显示,但在 Safari 或 iPhone 中不显示

ios - 核心数据未保存到 TableView