如何更新Core Data上的用户界面?

时间:2016-01-25 02:54:43

标签: swift uitableview core-data nsfetchedresultscontroller

我有一个带有UITableView,Core Data和NSFetchedResultsController的应用程序。我已将数据传递给DetailViewController。我可以从DetailViewController中删除它们!在Apple的iOS Notes应用程序中,您可以看到我想要的功能!当您从DetailViewController中删除备注时(例如),删除对象和Notes应用程序会自动显示下一个或前置备注!我想创建诸如函数。删除当前对象后如何更新用户界面?这是我的代码!谢谢` 导入UIKit 导入CoreData

class DetailViewController:UIViewController {

@IBOutlet weak var containerLabel: UILabel!

var retrieveData:NSManagedObject!
var managedObjectContext:NSManagedObjectContext!
var manager:Manager!

override func viewDidLoad() {
    super.viewDidLoad()
// Do any additional setup after loading the view.

    self.containerLabel.userInteractionEnabled = false
    self.containerLabel.textColor = UIColor.redColor()
    self.containerLabel.alpha = 0

    UIView.animateWithDuration(2.5) { () -> Void in
        self.containerLabel.alpha = 1
    }


    if let demo = self.retrieveData.valueForKey("titleField") as? String {
     self.containerLabel.text = demo
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func backToMain(sender: AnyObject) {
    // Back to the MainTableViewController
    self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func trashButton(sender: AnyObject) {

    self.managedObjectContext.deleteObject(retrieveData)

    do {
    try self.managedObjectContext.save()
    } catch {

    }
    self.dismissViewControllerAnimated(true, completion: nil)


}

`

如果我在列表中有5个项目,那么:

enter image description here

当我从列表中选择第四个项目时(例如)。而detailVC显示我选择的项目如下:

enter image description here

我想删除它们。当我删除"四"然后我的containerLabel.text显示列表中的先前对象。他们在"四"之后被删除,"三","二"和"一个"同样。在" One"删除我的containerLabel.text显示字符串

enter image description here

但我留下了一个名为" Five"

的单个对象

enter image description here

我的问题是"五"!我无法删除它。示例:在iOS Notes应用程序中,如果列表中有五个对象,例如我的演示应用程序。从列表中选择第四个对象时(例如)。在" Four"之后开始删除它们。是删除iOS Notes应用程序显示"五"。和"五" (列表中的最后一个对象)被删除,然后iOS Notes应用程序显示"三","二"和"一个"。也许问题就在这里:

if index != 0 { self.retrieveData = fetchedObject[index! - 1] } else { self.retrieveData == fetchedObject[0] }

enter image description here

2 个答案:

答案 0 :(得分:2)

让我们在这里采取简单(但不那么优雅)的路线。您必须将所有获取的对象传递给详细信息VC,如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "yourSegueIdentifier"{

    if let destinationVC = segue.destinationViewController as? DetailViewController{

        destinationVC.managedObjectContext = yourContext
        destinationVC.retrieveData = yourManagedObject
        destinationVC.arrayOfFetchedObjects = yourFetchedResultsController.fetchedObjects
        //pass over other data...

        }
    }
}

然后,在您的detailVC中,编写一个按下删除按钮时将执行的方法。像这样:

@IBAction func trashButton(sender: AnyObject) {

//make sure you have an array with YourObjects
guard let fetchedObjects = arrayOfFetchedObjects as? [YourObjectType] else {return}

//get index of the shown object in the array of fetched objects
let indexOfObject = fetchedObjects.indexOf(retrieveData)

//delete the object from the context
self.managedObjectContext.deleteObject(retrieveData)

do {
    try self.managedObjectContext.save()

    //delete the object from the fetchedObjects array
    fetchedObjects.removeAtIndex(indexOfObject)
} catch {

}

//get the object that should be shown after the delete
if indexOfObject != 0{
    //we want the object that represents the 'older' note
    retrieveData = fetchedObjects[indexOfObject - 1]
    updateUserInterface(true)

}
else{
    //the index was 0, so the deleted object was the oldest. The object that is the oldest after the delete now takes index 0, so just use this index. Also check for an empty array.

    if fetchedObjects.isEmpty{ 
         updateUserInterface(false)
     }
    else{
        retrieveData = fetchedObjects[0]
        updateUserInterface(true)
    }

    }
}


func updateUserInterface(note: Bool){

switch note{

    case true:

        //update the user interface
        if let demo = retrieveData.valueForKey("titleField") as? String {
            self.containerLabel.text = demo
        }

    case false:

        self.containerLabel.text = "no more notes"

    }
}

答案 1 :(得分:1)

您需要传递详细信息视图控制器

  1. 所有托管对象的列表以及列表中起始位置的索引
  2. 当前托管对象和获取下一个对象的回调
  3. 为了让它有足够的信息来做你想做的事。回调方法是最好的,是一种简单的委托形式,其中主视图控制器是提供额外数据的委托。