icestorm0806
icestorm0806

Reputation: 711

Pass coredata to ViewController based on cell selection

I need to present a new ViewController when selecting a UICollectionView Cell and pass the data from the entity used to fill selected cell.

Here is the code used to fill cell data:

   let pets = PersistenceManager.shared.fetch(Pet.self)
     var _fetchResultsController: NSFetchedResultsController <Pet>?
    var fetchResultsController: NSFetchedResultsController <Pet>?{
        get{
            if _fetchResultsController == nil {

                let moc = PersistenceManager.shared.context
                moc.performAndWait {
                    let fetchRequest = PersistenceManager.shared.petsFetchRequest()
                    _fetchResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil) as? NSFetchedResultsController<Pet>
                    _fetchResultsController?.delegate = self
                    do {
                        try self._fetchResultsController?.performFetch()
                    }catch {
                    }
                }
            }
            return _fetchResultsController
        }
    }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionViewHorizontal.dequeueReusableCell(withReuseIdentifier: "HorCell", for: indexPath) as! PRMainHorizontalCollectionViewCell

        if let pet= self.fetchResultsController?.fetchedObjects, indexPath.row < pet.count{
            let _pet= fetchResultsController!.object(at: indexPath)
// cell UI goes here
        }
        return cell
    }

I understand I need to use didSelectItemAt, I just don't know what information needs to go in the function. Please let me know of anything else needed to better help answer this question. Thank you.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

// Added the line below based on karthik's answer. But I am unsure how to implement it.
    let selectedObj = fetchResultsController!.object(at: indexPath)

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "selectedPetViewController") as! PRSelectedPetViewController

    navigationController?.pushViewController(vc, animated: true)

}

Upvotes: 0

Views: 241

Answers (2)

dronpopdev
dronpopdev

Reputation: 817

I prefer the following architecture:

This is the main controller with data. For a better understanding, I will simplify the data source.

class ViewController: UIViewController {
    // code ...

    @IBOutlet var collectionView: UICollectionView!
    fileprivate var data = [Pet]()
}

extension ViewController: UICollectionViewDataSource {
    // code ...

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorCell", for: indexPath) as? PRMainHorizontalCollectionViewCell else {
            return UICollectionViewCell()
        }

        let pet = data[indexPath.row]
        // TODO: configure cell using pet ...
        return cell
    }

}

extension ViewController: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let row = indexPath.row
        let pet = data[row]

        // TODO: Get the child controller in any way convenient for you.
        let childController = ChildViewController()
        // With the help of the delegate, we will receive notification of changes pet.
        childController.delegate = self

        // Thus, we pass the data to the child controller.
        childController.pet = pet
        childController.indexPath = indexPath

        // TODO: Present the view controller in any way convinient for you.
    }

}

extension ViewController: ChildViewControllerDelegate {

    func saveButtonPressed(_ controller: ChildViewController) {
        guard let pet = controller.pet, let indexPath = controller.indexPath else {
            return
        }

        // We save data and reload the cell whose data we changed.
        self.data[indexPath.row] = pet
        collectionView.reloadItems(at: [indexPath])
    }

    func cancelButtonPressed(_ controller: ChildViewController) {
        // Do something if necessary...
    }

}

In addition to the controller, the child controller also provides a delegate protocol for notification of changes.

protocol ChildViewControllerDelegate {

    func saveButtonPressed(_ controller: ChildViewController)

    func cancelButtonPressed(_ controller: ChildViewController)

}

// This is the controller you want to show after selecting a cell.
// I assume that in the child controller there is a button to save and cancel.
class ChildViewController: UIViewController {

    var delegate: ChildViewControllerDelegate?

    // The object whose data we are editing in the controller.
    var pet: Pet!

    // The location of the object in the main controller.
    var indexPath: IndexPath!

    override func viewDidLoad() {
        // TODO: Configure user interface using self.pet
    }

    @IBAction func saveButtonPressed(_ button: UIButton) {
        delegate?.saveButtonPressed(self)
    }

    @IBAction func cancelButtonPressed(_ button: UIButton) {
        delegate?.cancelButtonPressed(self)
    }

}

Upvotes: 3

karthik
karthik

Reputation: 621

you can follow this to pass information to another view controller.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let selectedObj = fetchResultsController!.object(at: indexPath)
        // instantiate presenting view controller object
        // add one property (manange object) in your presenting viewcontroller
        // assign the selected object to that property
        // present the view controller
    }

Upvotes: 2

Related Questions