将coredata从tableview传递到另一个tableview

时间:2016-06-17 21:21:39

标签: ios swift uitableview core-data tableviewcell

我正在努力让我的护理数据填充我的第二个tableview控制器。数据填充第一个tableview,我可以选择一行,segue用于转到第二个表,但不填充标签。

我看了一遍,发现了较旧的样本或obj-c,但我无法弄清楚,所以任何帮助将这个n00b指向正确的方向都会有所帮助。

这就是我所拥有的,我想我错过了如何填充变量以在列表tableview中传递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?

    }
}

1 个答案:

答案 0 :(得分:1)

警告包含答案 - 只需更改

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
    }

如果您只想要一个单元格,则应使用静态表视图

根据您当前的问题了解更多信息

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中添加对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)
    }
}
相关问题