我有一个TableViewController显示我试图在用户选择某一行时传递给另一个视图控制器的核心数据。我不确定为什么这不起作用。我已经查看并查看了似乎正确的代码 - 但是要传递的数据没有填充 - 在我准备segue时 - 没有数据,视图Controller中没有任何内容。任何帮助“修复”我在这里缺少的东西都非常感谢!
TableViewController代码(源代码)
import UIKit
import CoreData
import Foundation
class MyEventsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var myEvents:[myEventsData] = []
var fetchResultController: NSFetchedResultsController!
func controllerWillChangeContent(controller: NSFetchedResultsController!) {
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!,atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!){
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
tableView.reloadData()
}
myEvents = controller.fetchedObjects as [myEventsData]
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "myEventsCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyEventsTableViewCell
let eventData = myEvents [indexPath.row]
cell.eventNameLabel.text = "Name: " + eventData.eventName
cell.startDateLabel.text = "Start: " + eventData.startDate
cell.endDateLabel.text = "End: " + eventData.endDate
cell.backgroundColor = UIColor.clearColor()
return cell
}
override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showEventDetails" {
let destination = segue.destinationViewController as MyEventDetailsViewController
if let indexPath = self.tableView?.indexPathForCell(sender as MyEventsTableViewCell) {
let object = fetchResultController?.objectAtIndexPath(indexPath) as? myEventsData
destination.name = object?.eventName
destination.start = object?.startDate
destination.end = object?.endDate
}
}
}
ViewController代码(目的地)
import UIKit
import Foundation
class MyEventDetailsViewController: UIViewController {
@IBOutlet weak var eventName: UILabel!
@IBOutlet weak var startDate: UILabel!
@IBOutlet weak var endDate: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var postComment: UIButton!
@IBOutlet weak var checkIn: UIButton!
@IBOutlet weak var addPhoto: UIButton!
@IBOutlet weak var myWineJournal: UIButton!
var name: String?
var start: String?
var end: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
eventName.text = name
startDate.text = start
endDate.text = end
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}