ios - Swift - tableview.reloadData() 使应用程序崩溃

标签 ios swift uitableview

我正在从 plist 中获取一些数据到 UITableview 中。 但是,当我尝试重新加载数据以仅显示剩余的单元格时,我试图删除所选的数据,应用程序崩溃了。 我认为问题出在我使用 tableview.reloadData() 时,但我不确定如何解决此问题。如果我不使用 reloadData,当我重新打开 View Controller 时,单元格将被删除。

有什么建议吗?

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {

            let row = indexPath.row

            let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
            let DocumentsDirectory = plistPath[0] as! String
            let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist")
            let fileManager = NSFileManager.defaultManager()

            if (!fileManager.fileExistsAtPath(path)) {

                if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") {

                    let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
                    println("Bundle notes.plist file is --> \(resultDictionary?.description)")
                    fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
                    println("copy")

                } else {
                    println("notes.plist not found")
                }


            } else {
                println("note.plist already exists")

                //fileManager.removeItemAtPath(path, error: nil)
            }

            let resultDictionary = NSMutableDictionary(contentsOfFile: path)
            //resultDictionary?.removeAllObjects()
            resultDictionary?.removeObjectForKey(allKeys[row])
            resultDictionary!.writeToFile(path, atomically: false)

            println("Loaded notes.plist file is --> \(resultDictionary?.description)")


            tableView.reloadData()

        }
    } 

enter image description here

最佳答案

关于调用 reloadData(),文档说:“不应在插入或删除行的方法中调用它,尤其是在通过调用 beginUpdates 和 endUpdates 实现的动画 block 中。”所以最好重新加载您进行更改的部分,如果涉及动画,则调用开始和结束

tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths(path, withRowAnimation: UITableViewRowAnimation.Automatic)
tableView.endUpdates()

并且最好使用您自己的 nsfilemanager 实例,因为默认实例仅在主线程中工作。而且你在写入文件时不安全地解包 resultDictionary,这可能会导致崩溃 附言,

let path = DocumentDirectory.stringByAppendingPathComponent("notes.plist")

替换为stringByAppendingString n swift 2,仅供引用

关于ios - Swift - tableview.reloadData() 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790492/

相关文章:

ios - 奇怪的 UITableView 行为

ios - 在 iOS 中将通知从模型发送到 Controller

ios - 当文本有多行时,在UILabel中查找属性文本的宽度

iOS,打开一个推送通知,其他的也不见了

ios - 以编程方式添加 View 的问题

ios - 如何在纵向应用程序中录制横向视频? ( swift 2,iPhone)

ios - 缩放时 UITableViewCell 中的更新约束无法与 UIWebView scrollView 一起正常工作

ios - 成员类型的快速重载运算符

ios - 从 tableView 中选定的行打开 Map AnnotationView

ios - UIScrollview 中的 UIView 中的 UITableView : On tap in UITableView data gets cleared