在 Swift 中删除表格视图的核心数据记录(行)
Posted
技术标签:
【中文标题】在 Swift 中删除表格视图的核心数据记录(行)【英文标题】:Delete a coredata record(row) of a tableview on Swift 【发布时间】:2015-07-09 10:01:56 【问题描述】:我有点卡在这里。在这里从 Stack 尝试了许多帖子和想法,但无法从表格视图中删除内容。既不会从 COREDATA 中删除记录,也不会删除简单的测试数组。除此之外,代码工作正常,列出数据并在表格视图中显示。
这是我得到的错误:
2015-07-09 21:49:24.881 PlaygroundApp[36985:1319445] * 断言 -[UITableView _endCellAnimationsWithContext:] 中的失败, /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1623 2015-07-09 21:49:24.889 PlaygroundApp[36985:1319445] * 终止应用程序由于 未捕获的异常“NSInternalInconsistencyException”,原因: '无效更新:第 0 节中的行数无效。
import Foundation
import UIKit
import CoreData
class MyTableViewController: UIViewController, UITableViewDelegate
var cellCount = Int()
var selectedCell = Int()
var totalResults = Int()
// THIS VARIABLE IS INITIALISED BY COREDATA FETCHREQUEST THEN USED TO POPULATE CELLS
var recipients = [Float]()
//THIS IS JUST A TEST VAR TO TEST THE TABLE VIEW DELETE
var recipientsTeste = [1,2,3,4,5,6,7,8]
override func viewDidLoad()
super.viewDidLoad()
// FETCH COREDATA
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "ConcreteEntity")
request.returnsObjectsAsFaults = false
var fecthResults = context.executeFetchRequest(request, error: nil)!
//PRODUCE ARRAY FROM COREDATA
if fecthResults.count > 0
var saydir = fecthResults.count - 1
for (var i=0; i < fecthResults.count; i++)
let match = fecthResults[i] as! NSManagedObject
var tela = match.valueForKey("costAt") as! Float
println(tela)
recipients.append(tela)
else
//DEFINE # OF RECORDS AT COREDATA
totalResults = fecthResults.count
// NUMBER OF CELLS BASED ON # OF RECORDS AT COREDATA
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return totalResults
// TRANSFER COREDATA RECORDS TO CELLS
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
// LABEL CELL
cell.textLabel?.text = "\(recipientsTeste[indexPath.row])"
return cell
// IDENTIFY CLICKED CELLS
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
selectedCell = indexPath.row
println(selectedCell)
// DEFINE AN ACTION LATER TO USE THE SELECTED ROW AND CLOSE VIEW
if indexPath.row == 0
println("I clicked ZERO")
navigationController?.popViewControllerAnimated(true)
// ENABLE EDIT
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
return true
// EDIT CELL
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
//// I GET THIS INFO PRINTED CORRECTLY. IT CALLS THE METHOD FINE.
// println("delete indexPath \(indexPath.row)and \(recipients[indexPath.row])")
//****************** THIS IS THE BIT THAT DOESNT WORK **********************
switch editingStyle
case .Delete:
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "ConcreteEntity")
request.returnsObjectsAsFaults = false
var fecthResults = context.executeFetchRequest(request, error: nil)!
context.deleteObject(fecthResults[indexPath.row] as! NSManagedObject)
fecthResults.removeAtIndex(indexPath.row)
context.save(nil)
tableView.reloadData()
// remove the deleted item from the `UITableView`
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
default:
return
// *************************************************************************
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
【问题讨论】:
在 removeAtIndex 之后使用 fecthResults.count 更新您的“totalResults”变量 【参考方案1】:您正在重新加载表格,这将删除您尝试删除的行,然后您尝试再次删除这些行。移除对tableView.reloadData()
的调用。
tableView.reloadData() // Delete this line
// remove the deleted item from the `UITableView`
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
编辑:您应该查看 NSFetchedResultsController 它是为这种东西制作的。
【讨论】:
谢谢你的小费。我做了一些研究,发现了一个非常好的教程。不是专门针对 NSFetchedResultsController 的。但是他们解释了我终于明白了每一位的作用。 jamesonquave.com/blog/core-data-in-swift-tutorial-part-1以上是关于在 Swift 中删除表格视图的核心数据记录(行)的主要内容,如果未能解决你的问题,请参考以下文章