使用动画删除UITableViewCell(错误/崩溃) [英] delete UITableViewCell with animation (error/crash)

查看:139
本文介绍了使用动画删除UITableViewCell(错误/崩溃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除 UITableViewCell &来自具有动画的 UITableView 的CoreData对象。我写了这段代码:

  func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){

let mySelectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

// 1
让appDelegate = UIApplication.sharedApplication()。委托为AppDelegate

让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 

崩溃行: code> self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Right)



有人知道我做了什么错误?



更新

  tableView.beginUpdates()

var numberItems:Int = people.count

self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Right)
numberItems - = [indexPath] .count //类似[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] ***在 -  [UITableView _endCellAnimationsWithContext:]中的声明失败,/SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582 

在这行代码: tableView.endUpdates()

解决方案

如果表视图使用获取结果控制器,那么你有
才能删除对象的只有一件事是删除对象从托管对象上下文:

  let context = self.fetchedResultsController.managedObjectContext 
context.deleteObject(self.fetchedResultsController .objectAtIndexPath(indexPath)as NSManagedObject)

当然保存上下文以使更改永久。 / p>

然后通过
获取结果控制器委托方法自动更新表视图

  func controllerWillChangeContent(controller:NSFetchedResultsController)
func controller(controller:NSFetchedResultsController,didChangeSection sectionInfo:NSFetchedResultsSectionInfo,atIndex sectionIndex:Int,forChangeType type:NSFetchedResultsChangeType)
func controller :NSFetchedResultsController,didChangeObject anObject:AnyObject,atIndexPath indexPath:NSIndexPath ?, forChangeType type:NSFetchedResultsChangeType,newIndexPath:NSIndexPath?)
func controllerDidChangeContent(controller:NSFetchedResultsController)

您可以在
NSFetchedResultsControllerDelegate 文档,如果您没有
已经实现它们。



此外,您不应该在
people 数组中有一个副本的获取对象。获取的结果控制器在表视图中直接使用
数据源方法,例如:

  override func numberOfSectionsInTableView tableView:UITableView) - > Int {
return self.fetchedResultsController.sections?.count? 0
}

override func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > int {
let sectionInfo = self.fetchedResultsController.sections![section] as NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}

override func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Cell,forIndexPath:indexPath)as UITableViewCell
self.configureCell(cell,atIndexPath:indexPath)
return cell
}

如果您在Xcode
中创建一个全新的Master-Detail + Core Data Application将获得您需要的所有必要的模板代码。


I'd like to delete a UITableViewCell & a CoreData Object from a UITableView with an animation. I wrote this code for doing it:

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)
}

I don't know why but the app is crashing if I select a cell, which I want to delete.

2015-03-29 18:00:10.776 MyApp[3001:79507] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582

Crashing line: self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)

Does someone knows what I've done wrong?

Update:

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
}

New error:

2015-03-29 19:14:15.972 MyApp[3389:89566] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582

in this line of code: tableView.endUpdates()

解决方案

If the table view displays Core Data objects using a fetched results controller then the only thing you have to do to delete an object is to delete the object from the managed object context:

let context = self.fetchedResultsController.managedObjectContext
context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject)

and of course save the context to make the change permanent.

Updating the table view is then done "automatically" by the fetched results controller delegate methods

func controllerWillChangeContent(controller: NSFetchedResultsController)
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
func controllerDidChangeContent(controller: NSFetchedResultsController)

You'll find a typical implementation of these methods in the NSFetchedResultsControllerDelegate documentation, if you haven't implemented them already.

Also you should not have a "copy" of the fetched objects in your people array. The fetched results controller is directly used in the table view data source methods, for example:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.fetchedResultsController.sections?.count ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionInfo = self.fetchedResultsController.sections![section] as NSFetchedResultsSectionInfo
    return sectionInfo.numberOfObjects
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    self.configureCell(cell, atIndexPath: indexPath)
    return cell
}

If you create a fresh "Master-Detail + Core Data Application" in Xcode then you'll get all the necessary template code that you need.

这篇关于使用动画删除UITableViewCell(错误/崩溃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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