gpt4 book ai didi

swift - CoreData - 从 tableView 中删除行

转载 作者:行者123 更新时间:2023-11-30 10:12:33 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com