我有点卡在这里。在Stack中尝试了很多帖子和想法,但无法从表格视图中删除内容。无法从COREDATA或简单的测试阵列中删除记录。除此之外,代码工作正常,在tableview中列出数据和显示。
这是我得到的错误:
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()
}
}
答案 0 :(得分:0)
您正在重新加载表,这将删除您尝试删除的行,然后再次尝试删除行。移除对tableView.reloadData()
的电话。
tableView.reloadData() // Delete this line
// remove the deleted item from the `UITableView`
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
编辑:你应该研究NSFetchedResultsController为此做的事情。