【发布时间】:2023-12-09 03:54:01
【问题描述】:
我的核心数据项目中有 2 个 VC。在 VC1 中,我有一个表格视图,用于显示我的 coredata 数据库中的名称。在我的 VC2 中,我输入了实体的详细信息。现在我想在用户选择特定行时更新我的数据库。当用户选择一行时,它将移动到 VC2 并在文本字段中显示名称,如果用户更新其中的任何内容,它将更新。我在下面有我的代码。请建议我接下来应该做什么。
输入详细信息-
import UIKit
import CoreData
class EnterDetailViewController: UIViewController {
@IBOutlet weak var nametextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func savedata(sender: AnyObject)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedcontext = appdelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedcontext)
let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedcontext)
person.setValue(self.nametextfield.text, forKey: "name")
do
{
try managedcontext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
VC1-在表格视图中显示
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
var people = [NSManagedObject]()
let manai = UIApplication.sharedApplication().delegate as! AppDelegate
//var person :NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool)
{
print("CALLING FETCHDATA")
self.fetchData()
print("RELOADING DATA")
self.tableview.reloadData()
}
func fetchData()
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Person")
do
{
people = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
print(people)
print("FETCHING DATA")
}
catch let error as NSError
{
print("could not fetch \(error), \(error.userInfo)")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return people.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
let person = people[indexPath.row]
cell.textLabel?.text = person.valueForKey("name") as? String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
if editingStyle == .Delete
{
managedContext.deleteObject(people[indexPath.row])
}
do
{
try managedContext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
}
fetchData()
self.tableview .reloadData()
print("reload after delete")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "segueupdate"
{
}
}
}
【问题讨论】:
-
我不确定这是否是您的问题,但这对我来说看起来很奇怪:
var managedobjectt = NSManagedObject()。如果换成var managedobjectt: NSManagedObject?会怎样?