Passing Core Data Objects from UItableviewcell to Another View Controller

How to pass selected uitableviewcell core data object to a new view controller?

Just grab the right item in tableView(:didSelectRowAtIndexPath:) and pass it to the view controller, then perform the segue:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
// Setup the detail view controller with the item to display
// It should be defined as `var item: Item!` in DetailViewController
let detailViewController = DetailViewController()
detailViewController.item = items[indexPath.row]

// This will perform the segue you define in IB, with the view controller pre-loaded and configured
detailViewController.performSegueWithIdentifier("detailSegue", sender: self)
}

Passing Core Data objects from UITableViewCell to another View Controller

A little confused by what you're asking but from what I understand, once the user clicks on a cell you want to segue to another view controller where they can edit the the details?

To add an exception breakpoint, open up your left panel with all your files/viewcontrollers, at the very top of it should be a small panel with a few icons, the first one is a folder, click on the second last one (the one that looks like a tag)
click on the plus in the bottom right and click add exception breakpoint, this should let you know where the problem in your code is occurring

Okay to edit the details in another View controller start by preparing the segue from the original view controller

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

if segue.identifier == "showTaskDetail" {
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
let indexPath = self.tableView.indexPathForSelectedRow()
let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
detailVC.detailTaskModel = thisTask
detailVC.delegate = self

}
else if segue.identifier == "showTaskAdd" {
let addTaskVC:AddTaskViewController = segue.destinationViewController as! AddTaskViewController
addTaskVC.delegate = self
}
}

okay so like the code is showing above, I have a segue called showTaskDetail which shows the details of whatever it is, in my case its a simple task. You said you want to edit this information in another view controller when a user clicks on the row, well you need to be able to get this information in the other view controller.

So create a variable in the other viewcontroller that will hold these values, for me i called it detailTaskModel

 var detailTaskModel: TaskModel!

Incase you're confused about what TaskModel is, I'm using CoreData to store my data, and TaskModel is a NSMangedObject class.

let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController

this line you're just specifying what your other view controller is, replace TaskDetailViewController with your swift class.

let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController

this line fetches the data from the row selected

Now you should be able to pass your information into the other view controller and edit it using the detailTaskModel, do you need help with that as well?

Passing Core Data objects to another view controller when selecting row from UITableView

This is how I do it:

At the top of my class I'll have a var that stores the selected item:

var selectedTask: Task?

Then in my tableViews didSelectRow method:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedTask = tasks[indexPath.row]
performSegueWithIdentifier("showTaskDetail", sender: self)
}

And finally:

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

if segue.identifier == "showTaskDetail" {
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
detailVC.detailTaskModel = selectedTask
detailVC.delegate = self
}
}

However, I'd be more inclined to just pass some kind of ID/name and perform another fetch with a new moc rather than pass around core data objects.

You'll get less threading issues this way and it will make things far easier should you need to deep link into your app, from say, a notification or something.

Mocs are cheap, use them once, and throw them away.

Passing a Struct Array through a TableViewCell to another ViewController

If you want to share specific cell's data into table of other view controller, then you need to create an object of Model struct rather than its array just like below:

var newFeed: Model?

Next you can assign it value of particular cell before navigation in main did select method as below:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Comment") as? Comment else {return}
vc.newFeed = getInfo[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}

It will assign that cell's data to newFeed Variable and relaod table with that data.

For any question, feel free to ask.

Pass data between TableView and another ViewController in Swift

Your prepareForSegue must override the UIViewController method which is

func prepare(for segue: UIStoryboardSegue, sender: Any?)

Therefore your method in NewsfeedViewController should be

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondViewController = segue.destination as? EventViewController
secondViewController?.data = sender as? eventStruct
}

it would also be a good idea to add some error checking

Pass core data from selected table cell to new view controller

You need to track which item the person clicked.

var mySelection: Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
mySelection = indexPath.row
}

Then, use that when doing the segue.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)     {
if (segue.identifier == "RecipeDetail") {
//I WANT TO PASS THE DATA FROM THE TABLE CELL TO THE NEW VIEW CONTROLLER (RECIPEDETAILVC)
let recipeDetailControler = segue.destinationViewController as! RecipeDetailViewController

if let mySelection = mySelection {
let recipe = recipes[mySelection]
// add this function to your
recipeDetailControler.configureRecipeData(recipe)
}
}
}

Add this function to RecipeDetailViewController:

func configureRecipeData(recipe: Recipe) {

// IMPLEMENT ME

}


Related Topics



Leave a reply



Submit