繁体   English   中英

从TableView删除项目时出现AppDelegate错误

[英]AppDelegate error when remove item from TableView

我试图从TableView中删除一个项目,但是当我向左滑动并单击Delete时 ,出现此错误:

滑动时出错

这是我拥有的代码,当我注释self.tableView.deleteRowsAtIndexPaths行时,一切对我来说都很好。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            if let index: NSIndexPath = indexPath {
                self.tableView.beginUpdates()

                self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
                loadProductsToListOrder()

                self.tableView.endUpdates()
            }
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }

我搜索并在此处找到有关beginUpdatesendUpdates堆栈溢出信息。

谢谢。

更新资料

loadProductsToListOrder函数

func loadProductsToListOrder() {
        self.products = ProductDatabase.listProduct(self.configurations.db!)

        self.tableView.reloadData()
    }

如果我的TableView中有一个元素,则不会出现此问题。 如果我的TableView中有多个iten,就发生这种情况。

更新2

@harshayarabarla给出的答案对我有用,因为我忘记了将self.products.removeAtIndex(indexPath.row)添加到deleteRowsAtIndex之前。

谢谢!

有关完整的历史记录,请参阅从Coredata Swift删除数据

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

           //tableView.reloadData()
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        default:
            return

        }
}

还需要对这两行代码进行更正。

    var myData: Array<AnyObject> = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext

在执行self.tableView.deleteRowsAtIndexPaths之前,需要从表视图的数据源中删除对象。 稍后,您可以调用self.tableView.reloadData方法,而不是开始更新和结束更新。 不过不必调用reloadData方法,因为self.tableView.deleteRowsAtIndexPaths负责重新加载东西。

如果在对表视图的dataSource进行更改之前调用self.tableView.deleteRowsAtIndexPaths,则可能会遇到此错误。

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

                self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
                self.products.removeAtIndex(index.row)
                self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                loadProductsToListOrder() // you can comment this if you are not adding any new items to products array
            }
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM