Reputation: 13181
Problem
As illustrated, I have a container view (B) which resides inside of a view with other controls on it (A). The container view (B) holds a collection view, which I would like to update whenever a button is pressed on view (A).
I have gone through the UICollectionView Basics but feel I must be missing something. My natural response when wanting to communicate between UIViewControllers is to go off and build something based on either a callback or other delegate mechanism. Before I reinvent the wheel, any thoughts?
Currently when I click the buttons in the view, my collection data is updated and I call setNeedsDisplay and reloadData on the collection view (accessed via the childViewControllers property). I've tried calling setNeedsDisplay on the container view itself as well (no joy).
BTW - I have reviewed similar SO questions, which do not provide a matching use-case but do seem to indicate a lack of insight on this particular type of issue (if I've missed a great answer please let me know):
Solution
Please note that I've shared my solution below but additional answers are still welcome (especially if it's a better way)
Upvotes: 1
Views: 1849
Reputation: 13181
Solution
Since my search of the community showed marginal information on this topic, I am sharing my solution. I added a callback function on my data controller. Then upon closer inspection of How to tell UICollectionView about your content, I realized that I could simplly toggle the data source as indicated below (so I did a combination of both):
Please note the multiple options mentioned on how to get a UICollectionView to update itself.
Details
In my .h file for my data controller
//above @interface
typedef void (^CallbackBlock)();
//within @interface
@property (strong, nonatomic) CallbackBlock onDataUpdated;
//within my data controller method where data was changed
[DataManager sharedManager].onDataUpdated(); //singleton, i know...
//alternative to above singleton
__weak DataManager* weakSelf = [[DataManager alloc]init];
weakSelf.onDataUpdated();
//In my UIViewController (B) subclass
@property (strong,nonatomic)DataManager* controller;
//In my view did load of UIVC (B)
self.controller = [DataManager sharedManager];
__weak ProgressViewController* weakSelf = self;
self.controller.onDataUpdated = ^(){
//perform my action
[weakSelf setData:nil]; //remove existing data
[weakSelf setData:[self getSomeData]]; //get some new data
[[weakSelf progressCollectionView]reloadData];
[[weakSelf progressCollectionView] setNeedsDisplay];
};
End result:
Upvotes: 2