使用 Swift 中的 commitEditingStyle 动态删除 UITable 部分 [英] Delete UITable sections dynamically with commitEditingStyle in Swift

查看:28
本文介绍了使用 Swift 中的 commitEditingStyle 动态删除 UITable 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m dealing with an issue I can’t work around… I have a table of names, from a DB-array of customers, every customer has a name property among other data members.

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

I can delete rows within a section successfully, but what I can’t do it's deleting the section (when the last row within that section gets deleted, section must disappear).

我得到了:

'NSInternalInconsistencyException',原因:'无效更新:无效节数.表中包含的节数更新后的视图(3)必须等于节数包含在更新前的表视图中(4),加上或减去插入或删除的节数(插入 0 个,删除 0 个).

'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 数据源方法吗?

I know the table does some sanity checking behind the scenes of the data and this should match, but I can’t figurate exactly when, before calling deleteRowsAtIndexPaths? after? When should I update my property and/or dictionary? Should I manage numberOfSectionsInTableView data-source method?

我再说一遍,对于删除它的行,它工作正常,表移出该行并正确更新.部分的最后一行是交易...

I repeat, for rows deleting it’s working alright, the table moves out the row and gets updated properly. Last row on section is the deal...

我想我遗漏了一些东西,这就是为什么我要问......也找不到任何帮助阅读.

I guess I'm missing something, that’s why I’m asking… Couldn’t find any help reading around either.

非常感谢大家!

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
    }

推荐答案

您即将完成,但您需要一种方法来检测某个部分已经消失,以及哪个部分消失了,您可以调用 <代码>删除部分

You're almost there but you are going to need a way of detecting that a section has vanished, and which one has gone at which point you can call deleteSections

beginUpdate/endUpdate 调用中将更新部分括起来,但不要调用 reloadData(有关这些方法,请参阅文档)

Bracket the update section in a beginUpdate / endUpdate call but do NOT call reloadData (See the docs for those methods about it)

/**
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 可能会很棘手.

Without seeing the rest of your code , this should work but tableViews can be tricky when doing vanishing sections.

这篇关于使用 Swift 中的 commitEditingStyle 动态删除 UITable 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆