【发布时间】:2015-09-28 22:46:48
【问题描述】:
我正在从从 CoreData 接收其信息的 UITableView 中删除一行。目前,我已经弄清楚它可以删除 CoreData 信息的位置,但它并没有清除 UITableView 中的行。我使用的大多数方法都导致崩溃。这是原始代码(来源:blog.revivalx.com)
var peopleArray: NSMutableArray = [People]() //People is CoreData entity
@IBOutlet weak var contactTable: UITableView!
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext!
context.deleteObject(peopleArray[indexPath.row] as! NSManagedObject)
peopleArray.removeAtIndex(indexPath.row)
context.save(nil)
我试过插入:
contactTable.deleteRowsAtIndexPaths([indexPath.row
], withRowAnimation: UITableViewRowAnimation.Automatic)
进入此代码并遇到错误。对于如何同时执行这两种删除,大量的消息来源并没有为我提供答案。
如何在 Swift 中同时从 UITableView 中删除该行及其在 CoreData 中的对应条目?
谢谢
编辑:
这里是完整的函数:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext!
context.deleteObject(peopleArray[indexPath.row] as NSManagedObject)
peopleArray.removeAtIndex(indexPath.row)
contactTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
context.save(nil)
contactTable.reloadData()
}
}
【问题讨论】:
-
什么是
myList?它与peopleArray有什么关系?在numberOfRowsInSection和/或cellForRowAtIndexPath中使用了哪些?
标签: ios swift uitableview core-data