将核心数据从表视图传递到另一个表视图

Passing coredata from tableview to another tableview

我正在努力获取我的护理数据以填充我的第二个 table 视图控制器。数据正在填充第一个 table 视图,我可以 select 一行,segue 用于转到第二个 table 但标签未填充。

我四处寻找并找到了较旧的样本或 obj-c,但我无法弄清楚,因此任何帮助将此 n00b 指向正确方向的帮助都会有所帮助。

这是我所拥有的,我想我缺少如何填充变量以在列表 table 视图中传递 prepareForSegue,但我可能是错的。我在该函数中收到警告错误 (Warning cannot assign value of type 'ListEntity' to type '[ListEntity]')。

CoreData

Entity = ListEntity
Attributes = title, event & location (all as Strings)

listTableViewController

import UIKit
import CoreData

class ListTableViewController: UITableViewController {

    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    var lists = [ListEntity]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "The List"

        let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: #selector(ListTableViewController.addButtonMethod))
        navigationItem.rightBarButtonItem = addButton

    }

    func addButtonMethod() {
        print("Perform action")
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        reloadData()
        tableView.reloadData()
    }

    func reloadData() {
        let fetchRequest = NSFetchRequest(entityName: "ListEntity")
        do {
            if let results = try managedObjectContext.executeFetchRequest(fetchRequest) as? [ListEntity] {
                lists = results
            }
        } catch {
            fatalError("There was an error fetching the list!")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lists.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("ListCell") as! ListTableViewCell
        let list = lists[indexPath.row]
        cell.configurationWithSetup(list)

        return cell
    }

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

        performSegueWithIdentifier("DetailsSegue", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

         if segue.identifier == "DetailsSegue" {
            let destinationVC = segue.destinationViewController as! DetailsTableViewController
            let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
            print(indexPath.row) // Print the Row selected to console
            // Place the code to pass data here?
            // destinationVC.lists = lists[indexPath.row]
            // Warning cannot assign value of type 'ListEntity' to type '[ListEntity]'
          }
    }
}

listTableViewCell

import UIKit

class ListTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!

    func configurationWithSetup(list: AnyObject) {
        titleLabel.text = list.valueForKey("title") as! String?
    }
}

detailsTableViewController

import UIKit
import CoreData

class DetailsTableViewController: UITableViewController {
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    var lists = [ListEntity]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DataCell") as! DetailsTableViewCell
        let list = lists[indexPath.row]
        cell.configurationWithSetup(list)

        return cell
    }
}

detailsTableViewCell

import UIKit
import CoreData

class DetailsTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var eventLabel: UILabel!
    @IBOutlet weak var locationLabel: UILabel!

    func configurationWithSetup(list: AnyObject) {

        titleLabel.text = list.valueForKey("title") as! String?
        eventLabel.text = list.valueForKey("event") as! String?
        locationLabel.text = list.valueForKey("location") as! String?

    }
}

警告中包含答案 - 只需更改

var lists = [ListEntity]()var lists = ListEntity(),或 var lists:ListEntity!,当您准备 segue 时设置该值。

那你就需要改变

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DataCell") as! DetailsTableViewCell
        // as data source is not array you can just you the item you passed
        // let list = lists[indexPath.row]
        cell.configurationWithSetup(lists)

        return cell
    }

如果你只想要一个单元格,你应该使用静态 table 视图

每个当前问题的更多信息

class DetailsTableViewController: UITableViewController {
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

   var theDetailListEntity:ListEntity! 

    override func viewDidLoad() {
        super.viewDidLoad()
        print(theDetailListEntity) // check that you passed it across
    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DataCell") as! DetailsTableViewCell
        cell.configurationWithSetup(theDetailListEntity)
        return cell
    }
}

不要忘记在 listTableViewController 中添加 prepare for segue 否则 theDetailListEntity 将无法设置...然后它会崩溃。

根据您设置 segue 的方式,它可能会有所不同。但这就是你需要的

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("showMyDetailView", sender: indexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showMyDetailView" {
        guard let
            vc = segue.destinationViewController as? DetailsTableViewController,
            ip = sender as? NSIndexPath else { fatalError() }
        let item = lists[ip.row]
        vc.theDetailListEntity = item
        // set the item in the next VC
        tableView.deselectRowAtIndexPath(ip, animated: true)
    }
}