【发布时间】: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”变量
标签: swift uitableview core-data delete-row