How to edit a UICollectionView item in another UIViewController?

843 views Asked by At

I'm using a Master Detail View Controller with Swift. When I click on a master cell, I see a collection view containing a list of pictures. Each picture represent data stored inside Core Data. Each picture data has also a timestamp and all picture taken the same day are stored in the same Master cell.

[Date of the day 1]
   [pic1]
   [pic2]

[Date of the day 2]
   [pic3]
   [pic4]

What I want to achieve is: When I click on a picture, another ViewController is displayed (through a segue) and this new VC allows me to edit the picture and change its date.

The thing is I have multiple things happening here when I change the day or a picture:

  • if the pic was alone in the collection, then I also have to remove the Master Day cell too.
  • if the pic was not alone, I just need to update the detail collection view
  • In any cases, if I have to create a new Day, I need to create a new Master Cell and update the view and if I need to delete a Day, I have to also update the Master view.

If you edit the collection items, you can simply do a:

self.collectionView performBatchUpdates:

and then update the model, and notify the collection view of your action... but I don't see how I can do that from another UIViewController.

Currently, in the Edit View Controller, I update the model and then dismiss the View controller. And I get a crash because the collectionView tries to display an Item that is not part of the current collection anymore...

To solve this issue, maybe I can use a protocol in order to inform the detail view from the edit view but If I do that, I need to give the IndexPath of the Edit Cell to the edit view and then give it back to the detail view through the protocol... Why not. but not that clean in my opinion.

Another problem is, the app seems to also crash in the master view when I delete a Day. Because, the master view doesn't know that I deleted a day from the edit view.

My main problem is that I don't see how to tell my Master View Controller that I made some model changes... especially when I don't have any IndexPath information from within the edit view.

Technically, I could use the notificationCenter to notify both Views, I guess... but still, I would need to pass both IndexPath from the Master view to the Detail view and then to the Edit view.

Ugly.

3

There are 3 answers

0
Mikael On BEST ANSWER

I found an interesting article addressing my problem. It was not indeed a trivial problem...

http://www.objc.io/issues/4-core-data/full-core-data-application/#using-a-collection-view

2
Ankit Sachan On

Several ways by which this can be achieved:

1) Delegation: The view controller containing collection view will act as a delegate of Edit View Controller and when editing is done it will callback a method to 'The view controller containing collection view'

2) Notification: EditingFinished

3) blocks

0
Zell B. On

Using protocol is good solution but can be painful since you have to manage edited items using their indexPaths. A simpler solution, since you are using CoreData, is to fetch entries using a computed property and just call collectionView.reloadData() on viewWillAppear method from the controller that contains collectionView. This way calling reload data will recompute Core Data entries and will place each item in correct position. Also viewWillAppear will get called after dismissing controller that stands on top of collection view controller