【发布时间】:2015-03-29 15:56:29
【问题描述】:
我想用动画从UITableView 中删除一个UITableViewCell 和一个CoreData 对象。我为此编写了这段代码:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let mySelectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
//1
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let fetchRequest = NSFetchRequest(entityName:"Person")
//3
mySelectedCell.backgroundColor = green
mySelectedCell.detailTextLabel?.text = "Done"
mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
let person = people[indexPath.row]
managedContext.deleteObject(person)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
}
我不知道为什么,但如果我选择了一个我想删除的单元格,应用程序就会崩溃。
2015-03-29 18:00:10.776 MyApp[3001:79507] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582
崩溃线:self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
有人知道我做错了什么吗?
更新:
tableView.beginUpdates()
var numberItems:Int = people.count
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
numberItems -= [indexPath].count // something like [indexPath].count
tableView.endUpdates()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
application.applicationIconBadgeNumber = people.count
return people.count
}
新错误:
2015-03-29 19:14:15.972 MyApp[3389:89566] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582
在这行代码中:tableView.endUpdates()
【问题讨论】:
-
应用程序不只是崩溃。崩溃时收到的具体错误消息是什么?它在哪条线上崩溃了?
-
我更新了我的问题。
-
您使用的是抓取结果控制器吗?
-
是的,我正在使用获取结果控制器。
标签: ios uitableview swift core-data