Swift CoreData:显示特定数据的最佳方法?

时间:2016-03-14 16:52:37

标签: ios swift core-data

我做了搜索,仍然无法弄清楚如何做到这一点:

我有一个tableViewVC,它显示从CoreData Entity:Person中检索的数据列表(单元格)。单元格仅显示每个数据条目的人名属性。

当我点击单元格(func didSelectRowAtIndexPath)时,它将转到detailViewVC,我想在其中显示与该单个数据相关的其余属性的值(即年龄,性别,地址等)。 / p>

我原本打算将名称的字符串值从VC1传递给VC2,然后在VC2中进行某种循环,根据name属性搜索相关数据,但如果名称在数据库中重复,这不行。绝对是一个无法解决的愚蠢的解决方法。

所以我想使用每个数据条目的唯一ID,比如ObjectID?如果是这样,该怎么办?另一种更好,更有效的方式?

我是新人,所以请对我保持温柔和耐心。谢谢!

编辑:

VC1有tableview:

import CoreData

class vc1: UIViewController, UITableViewDataSource, UITableViewDelegate {
    ... ... ...

    var personData = [Person]()

    override func viewDidAppear(animated: Bool) {
        fetchAndSetResults()
        medTableView.reloadData()
    }

    func fetchAndSetResults() {
        let app = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = app.managedObjectContext
        let fetchRequest = NSFetchRequest(entityName: "Person")

        do {
            let results = try context.executeFetchRequest(fetchRequest)
            self.personData = results as! [Person]
        }

        catch let err as NSError {
            print(err.debugDescription)
        }
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCellWithIdentifier("PersonCell") as? PersonCell {

            let person = personData[indexPath.row]

            cell.configureCell(person) //custom func that shows the string value of attribute "name" of entity "Person" on an IBOutlet label.

            return cell
        }

        else {
            return UITableViewCell()
        }
    }
    ... ...

    // Tapping a cell will present VC2_DetailView.
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let indexPath = tableView.indexPathForSelectedRow;
        let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! PersonCell

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("VC2_DetailView") as! VC2_DetailView

        // Pass person's name in the tapped cell to the label.text in VC2_DetailView.
        vc.receivedPersonName = currentCell.cellPersonName.text

        //vc.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
        //self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

        // Adding dispatch_async to eliminate double tapping on cell. VC2 will be presented.
        dispatch_async(dispatch_get_main_queue(), {
            self.presentViewController(vc, animated: true, completion: nil)
        })
    }
    ... ...

}

这样,当点击一个单元时,将显示VC2。我想要实现的是VC2将显示与该特定人员姓名相关的其他属性,例如实体“人物”的“年龄”和“性别”,并将其显示在VC2中的相关IBOutlet标签中。

更多信息:VC1表有3个单元格:John,Jenny和Josh。当我点击Jenny时,VC2将出现并显示她的年龄和性别。 实体“人”有3个属性:“名称”,“年龄”和“性别”。

我是新人,非常感谢任何精心解释!

3 个答案:

答案 0 :(得分:3)

你可以直接传递实体。

答案 1 :(得分:0)

好吧,伙计们,没有人似乎有兴趣帮助我更深入。我以某种方式解决了我的问题:

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //let indexPath = tableView.indexPathForSelectedRow;
    //let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! PersonCell

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("VC2_DetailView") as! VC2_DetailView

    // Pass person's name in the tapped cell to the label.text in VC2_DetailView.

    // Get all data stored in the coredata[indexpath] on the same tapped cell.
    let person = personData[indextPath.row]
    vc.receivedPersonName = person.name
    vc.receivedPersonAge = person.age
    vc.receivedPersonGender = person.sex

    //vc.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
    //self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

    // Adding dispatch_async to eliminate double tapping on cell. VC2 will be presented.
    dispatch_async(dispatch_get_main_queue(), {
        self.presentViewController(vc, animated: true, completion: nil)
    })
}

基本上,我检索了特定数据的所有属性并将它们传递给VC2。

我的下一个挑战将是如何编辑这些数据并保存/更新现有数据,避免创建新数据或复制数据。

感谢所有人的快乐编码!

答案 2 :(得分:0)

好吧,我实际上简化了我的代码,以便在特定的indexpath.row上显示实体的详细信息。

单击单元格时:

// Let's try a new method by passing indexpath.row interger.
    let index = indexPath.row
    VC2.receivedIndex = index

因此,VC2将从VC1接收抽头单元的索引号。

然后我在VC2中再次实现了实体的fetchAndResults(),只使用收到的索引号,我就可以在相关的label.text(s)中显示该特定实体的所有细节。

对我来说,知道索引号(在我问你是否有唯一的ObjectID之前,但我相信索引号是唯一的ID)更好,所以稍后我可以实现现有细节的更新功能。 / p>

干杯!