根据单元格选择将coredata传递给ViewController

时间:2019-02-11 19:27:22

标签: ios swift core-data uicollectionview

选择UICollectionView单元格时,我需要展示一个新的ViewController,并传递用于填充所选单元格的实体中的数据。

以下是用于填充单元格数据的代码:

   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
    }

我知道我需要使用didSelectItemAt,我只是不知道该函数中需要输入什么信息。请让我知道其他需要什么来更好地帮助回答这个问题。谢谢。

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)

}

2 个答案:

答案 0 :(得分:2)

您可以按照此步骤将信息传递给另一个视图控制器。

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
    }

答案 1 :(得分:2)

我更喜欢以下架构:

这是带有数据的主控制器。 为了更好的理解,我将简化数据源。

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...
    }

}

除了控制器之外,子控制器还提供了一个委托协议来通知更改。

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)
    }

}