如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?

Posted

技术标签:

【中文标题】如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?【英文标题】:How to grab object id from UITableView and delete object based on object id? 【发布时间】:2015-02-10 02:23:08 【问题描述】:

现在我遇到了这个问题,我不知道在用户从中选择后如何从 UITableView 中获取对象 id 并将其传递给另一个视图控制器,我想根据我已经获取的对象 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)

    

删除视图控制器

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

【问题讨论】:

【参考方案1】:

我已经找到答案了。

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)

    

删除视图控制器

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)
                    

【讨论】:

【参考方案2】:

我通常使用两个函数来执行此操作,一个是获取特定用户,一个是删除用户。现在这些方法假设每个对象都有一个唯一的 id,所以使用下面的函数你只需调用deleteUser(fetchUser(someID))

func fetchUser(id: Int) -> User? 

    // Define fetch request/predicate
    var fetchRequest = NSFetchRequest(entityName: "User")
    let predicate = NSPredicate(format: "id == \(id)")

    // Assign fetch request properties
    fetchRequest.predicate = predicate
    fetchRequest.fetchBatchSize = 1
    fetchRequest.fetchLimit = 1

    // Handle results
    let fetchedResults = managedObjectContext?.executeFetchRequest(fetchRequest, error: nil)

    if fetchedResults?.count != 0 

        if let fetchedUser: User = fetchedResults![0] as? User 
            return fetchedUser
        
    
    return nil


// Delete
func deleteUser(id: Int) 
    if let user = fetchEvent(id) 
        managedObjectContext?.deleteObject(user)
    

【讨论】:

以上是关于如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何从获取的结果对象中填充稀疏的 UITableView?

如何通过滑动从 Parse 中的 UITableView 中删除对象?

如何从 UITableView 获取行索引,其中数组列表由追加到类

从具有 id 的 UItableview 单元格中获取数据

如何从 UITableView 推送 UIViewController

如何从 UIButton 操作方法获取 UITableViewCell 对象?