swift - CoreData - 从 tableView 中删除行

标签 swift uitableview core-data nsfetchedresultscontroller delete-row

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

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

所以,问题是每当我尝试从 TableView 中删除某些内容时,只有当它是 TableView 中的第一项时它才能正常运行。如果我尝试删除第一个以外的任何其他项目,应用程序就会崩溃,并显示数组索引超出范围

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

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

更新

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
    }

最佳答案

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

这可能会有所帮助。

关于swift - CoreData - 从 tableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182747/

相关文章:

core-data - 使实体关系可编码/可解码时出错

Swift - 在语音上下文数组中添加短语时崩溃

iPhone CoreData 应用程序首次启动时崩溃

ios - 出现时键盘高度会发生变化

iphone - UISearchBar 表部分索引标题重叠

ios - 在 xib 的每一行中使用 uitableview 自定义多个单元格

iphone - 框架代码将消息发送到已释放的 UITableViewController 对象的错误

swift - 将 NSSet 转换为数组

Swift 4 从后退按钮中删除文本

ios - ARKit图像识别——图像跟踪