CoreData - 从 tableView 中删除行

Posted

技术标签:

【中文标题】CoreData - 从 tableView 中删除行【英文标题】:CoreData - Deleting row from tableView 【发布时间】:2015-08-24 12:48:26 【问题描述】:

我需要一些有关此代码的帮助:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
        println("section and row \(indexPath.section) \(indexPath.row) ")
        if self.fetchedResultsController == nil 
            println("error when trying to delete object from managed object")

         else if (editingStyle == UITableViewCellEditingStyle.Delete) 
            moc?.deleteObject(detailsArray[indexPath.row] as NSManagedObject)
            detailsArray.removeAtIndex(indexPath.row)

            var error: NSError?
            moc?.save(&error)
        
    
// MARK: NSFetchedResultsControllerDelegate
    func controllerWillChangeContent(controller: NSFetchedResultsController) 
        self.exerciseTableView.beginUpdates()
    
    func controller(controller: NSFetchedResultsController,
        didChangeObject anObject: AnyObject,
        atIndexPath indexPath: NSIndexPath?,
        forChangeType type: NSFetchedResultsChangeType,
        newIndexPath: NSIndexPath?)
    
        switch type 
        case NSFetchedResultsChangeType.Insert:
            // Note that for Insert, we insert a row at the __newIndexPath__
            if let insertIndexPath = newIndexPath 
                self.exerciseTableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            
        case NSFetchedResultsChangeType.Delete:
            // Note that for Delete, we delete the row at __indexPath__
            if let deleteIndexPath = indexPath 
                self.exerciseTableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            
        case NSFetchedResultsChangeType.Update:
            // Note that for Update, we update the row at __indexPath__
            if let updateIndexPath = indexPath 
                let cell = self.exerciseTableView.cellForRowAtIndexPath(updateIndexPath)
                let details = self.fetchedResultsController!.objectAtIndexPath(updateIndexPath) as? TrainingDetails

                cell!.textLabel!.text = "\(details!.exerciseName)"
                cell!.detailTextLabel!.text = "Sets: #\(details!.setsNumber) Reps: #\(details!.repsNumber)"
            
        case NSFetchedResultsChangeType.Move:
            // Note that for Move, we delete the row at __indexPath__
            if let deleteIndexPath = indexPath 
                self.exerciseTableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            

            // Note that for Move, we insert a row at the __newIndexPath__
            if let insertIndexPath = newIndexPath 
                self.exerciseTableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            
            

    func controller(controller: NSFetchedResultsController,
        didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
        atIndex sectionIndex: Int,
        forChangeType type: NSFetchedResultsChangeType)
    
        switch type 
        case .Insert:
            let sectionIndexSet = NSIndexSet(index: sectionIndex)
            self.exerciseTableView.insertSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
        case .Delete:
            let sectionIndexSet = NSIndexSet(index: sectionIndex)
            self.exerciseTableView.deleteSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
        default:
            ""
        
    
    func controllerDidChangeContent(controller: NSFetchedResultsController) 
        exerciseTableView.endUpdates()
    

所以,问题是每当我尝试从表视图中删除某些内容时,它只有在它是表视图中的第一项时才能正常运行。如果我尝试删除不是第一个的任何其他项目,应用程序会崩溃并显示 Array index out of range

我不明白我的错误在哪里。有人可以帮我吗?

我希望我们能破解这个!提前致谢。

更新

cellForRowAtIndexPath 函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

        let cell = tableView.dequeueReusableCellWithIdentifier("exerciseCell", forIndexPath: indexPath) as! UITableViewCell
        let details = fetchedResultsController!.objectAtIndexPath(indexPath) as! TrainingDetails
        cell.textLabel!.text = "\(details.exerciseName)"
        cell.detailTextLabel!.text = "Sets: #\(details.setsNumber) Reps: #\(details.repsNumber)"

        return cell
    

【问题讨论】:

【参考方案1】:
  func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  

        println("section and row \(indexPath.section) \(indexPath.row) ") 
        if self.fetchedResultsController == nil  
             println("error when trying to delete object from managed object") 

         else if (editingStyle == UITableViewCellEditingStyle.Delete)  

         switch editingStyle  

               case .Delete: 
                     moc?.deleteObject(fetchedResultsController?.objectAtIndexPath(indexPath) as! TrainingDetails) 
                     moc?.save(nil) 

              case .Insert: 
                        break 
              case .None: 
                        break 
              
         
     

这可能会有所帮助。

【讨论】:

我不知道发生了什么,但现在它在这一行崩溃了** moc?.deleteObject(detailsArray[indexPath.row] as NSManagedObject)** 说数组索引超出范围! 可以添加deleteObject函数的代码吗?您是否使用 NSPredicate 来删除特定数据? 我没有“deleteObject”功能。我向您展示了有关删除表格视图中项目的代码!我还需要做什么? 注释“deleteObject”行并再试一次,并注释“moc?.save()”行。我也更新了答案。 如果我注释 deleteObject 行,崩溃发生在下一行:detailsArray.removeAtIndex(indexPath.row)。我们正在尝试从 detailsArray 中删除该对象。也许错误在那里?我们不应该尝试从 fetchedResultsController 中删除对象吗?

以上是关于CoreData - 从 tableView 中删除行的主要内容,如果未能解决你的问题,请参考以下文章

从 TableView 中获取数据(CoreData)

从 TableView 中的 CoreData 中删除实体时出错

从 CoreData 检索的 TableView 上的图像

从 coredata 中过滤值并分配给 tableview

填充 TableView 中的 JSON 数组和 CoreData

在tableview单元格上打印CoreData