gpt4 book ai didi

ios - 在 iOS9 swift 中选择特定行更新核心数据数据库

转载 作者:行者123 更新时间:2023-11-28 08:51:36 24 4
gpt4 key购买 nike

<分区>

我的核心数据项目中有 2 个 VC。在 VC1 中,我有一个 TableView ,用于显示我的 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-在表格 View 中显示

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"
{



}
}


}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com