此代码似乎正在删除实际数据,但是除非我强行关闭应用程序并重新打开,否则表格不会在没有行的情况下更新。 提前谢谢。
导入UIKit 导入CoreData
类ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
var lists = [NSManagedObject]()
var deleteListIndexPath: NSIndexPath? = nil
@IBOutlet weak var myTableView: UITableView!
@IBAction func addItem(sender: AnyObject) {
let alert = UIAlertController(title: "New List", message: "Add a new list", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default) { (UIAlertAction) -> Void in
let textField = alert.textFields![0] as UITextField
if textField != "" {
self.saveItem(textField.text!)
self.myTableView.reloadData()
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alert.addTextFieldWithConfigurationHandler(nil)
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return lists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let myCell:UITableViewCell = myTableView.dequeueReusableCellWithIdentifier("Prototype1")!
let list = lists[indexPath.row]
print(lists[indexPath.row])
myCell.textLabel?.text = list.valueForKey("listTitle") as? String
return myCell
}
override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.dataSource = self
self.myTableView.delegate = self
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName:"List")
// you may need to sort the list
let sortDescriptor1 = NSSortDescriptor(key: "listTitle", ascending: true)
let sortDescriptors = [sortDescriptor1]
fetchRequest.sortDescriptors = sortDescriptors
var fetchedResults : [NSManagedObject] = []
do
{
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
}
catch
{
return
}
for list in fetchedResults
{
lists.append(list)
}
self.myTableView.reloadData()
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete ) {
// Find the LogItem object the user is trying to delete
let logItemToDelete = lists[indexPath.row]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
managedContext.deleteObject(logItemToDelete)
self.myTableView.reloadData()
}
}
func saveItem(Saveitem: String)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: managedContext)
let item = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
item.setValue(Saveitem, forKey: "listTitle")
do {
try managedContext.save()
lists.append(item)
}
catch {
print("Error")
}
}
}
答案 0 :(得分:1)
您可以使用NSFetchedResultsController
类及其NSFetchedResultsControllerDelegate
协议方法。然后,您可以非常轻松地更新,删除,插入或移动对象!
如果您的tableView没有更新?您可以使用viewDidAppear
功能。
答案 1 :(得分:0)
我认为你仍然需要删除" logItem"来自列表对象。
lists.removeAtIndex(indexPath.row)
如果你从tableview数据源协议上传这个函数的代码,我可以给你一个更准确的答案:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)