gpt4 book ai didi

ios - 如何从 UITableView 中获取对象 ID 并根据对象 ID 删除对象?

转载 作者:行者123 更新时间:2023-11-28 09:13:49 25 4
gpt4 key购买 nike

现在我遇到了这个问题,我不知道如何在用户从 UITableView 中选择对象 ID 并将其传递到另一个 View Controller 后,我想根据我已经获取的对象 ID 删除对象从上一个。所有数据均来自user实体。

UITableView

var myList: Array <AnyObject> = []

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext!

/* grab object id here from core data object and pass to NSUserDefault*/
NSUserDefaults.standardUserDefaults().setObject(objectId, forKey: "userId")

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
self.presentViewController(deleteViewController, animated:true, completion:nil)

}

删除 View Controller

 let objectId = NSUserDefaults.standardUserDefaults().stringForKey("userId")
/* Grab object id from NSUserDefault and delete object here*/

最佳答案

我已经找到了答案。

UITableView

var myList: Array <AnyObject> = []

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let data: NSManagedObject = myList[indexPath.row] as NSManagedObject

let userId = data.valueForKeyPath("userId") as? String
NSUserDefaults.standardUserDefaults().setObject(objectId, forKey: "userId")

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
self.presentViewController(deleteViewController, animated:true, completion:nil)

}

删除 View Controller

let userId = NSUserDefaults.standardUserDefaults().stringForKey("userId")

//reference to our app delegate
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext!

let request = NSFetchRequest(entityName: "EntityOne")
request.includesSubentities = false
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "userId = %@", userId!)
let items = context.executeFetchRequest(request, error: nil)!

for item in items {
context.deleteObject(item as NSManagedObject)
}

关于ios - 如何从 UITableView 中获取对象 ID 并根据对象 ID 删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423025/

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