8

I'm trying to build an iPad app working with Core Data. But I'm facing a design and a coding issue. Let's say I add one UIViewController to my window and inside that viewcontroller, I need to display two tableviews (2 different entities) and 2 views (2 others entities) (So I need to fetch 4 entites for one UIViewController). I only found tutorials explaining how to use NSFetchedResultsController with one UITableView !

My question is : Should I declare 2 UITableView and 2 NSFetchedResultsController in my viewController ? Or should I declare 2 UITableViewController and then inside each of them declare one NSFetchedResultsController ? What about the two others views ? I should use NSFetchRequest ? If yes, where ? inside the views or inside the UIViewController ?

I know it's a lot a questions :). Thanks in advance for your help.

Dabrut
  • 862
  • 1
  • 10
  • 25

2 Answers2

8

Yes, you'll want to use 2 NSFetchedResultsController, one per UITableView. NSFetchedResultsController takes a NSFetchRequest, so you'll want to setup your NSFetchRequests based on the data that needs to be displayed in the UITableViews.

You should put all this controller logic inside your UIViewController subclass.

It is fine to have multiple UITableViews inside a single UIViewController, just keep in mind the delegate and data source methods. You'll have to test the UITableView parameter to see which table is asking for data/delegation handling.

logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • I have used 1 UITableView with 4 NSFetchedResultsControllers, 1 NSFetchedResultsController per section. It is working fine. So I would say this would be a simpler solution then multiple UITableViewControllers. – Ryan Oct 20 '11 at 15:01
  • @Ryan : You're saying it's easier to use only my UIViewController instead of 2 UITableViewControllers and 2 others UIViewController (for the 2 others UIView) ? – Dabrut Oct 20 '11 at 15:06
  • @Dave, Apple's View Controller Programming Guide [link](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html) states "You should not use multiple custom view controllers to manage different portions of the same view hierarchy." So that is my reason for not suggesting that. I think looking ahead you would find multiple UIViewControllers grow more complex if they have to interact in some form. Hard to say without knowing you exact situation. – Ryan Oct 20 '11 at 15:30
  • Oh OK I understand @Ryan . To know my situation, imagine a UIViewController in landscape (iPad). Inside this UiViewController, I'm adding has subviews : 1) a Tableview (square format) in the upper left (one entity). 2) another Tableview (square format) in the upper right (another entity). 3) a Uiview in the bottom left (another entity). 4) another UIView in the bottom right (another entity). Each UIView has 1 UIImageView and 3 UILabels. – Dabrut Oct 20 '11 at 15:48
  • I would suggest using a UIViewController, with two NSFetchedResultsControllers and two UITableViews, just as stated in this answer. You could look into implementing a Container View Controller, and using @Kevin's answer. But I don't have experience with those. – Ryan Oct 20 '11 at 16:00
  • I'll go with the two NSFetchedResultsControllers and two UITableViews solution. – Dabrut Oct 21 '11 at 08:22
  • You can use (1) two and more NSFetchedResultsControllers for multiple UITableViews or (2) for multiple sections in one UITableView. – Vladimír Slavík Jul 02 '14 at 18:16
  • In the case (1) you select the right controller in UITableViewDataSource/UITableViewDelegate methods using (UITableView*)table argument. In the NSFetchedResultsControllerDelegate methods use comparison against the controller argument to get the right UITableView. In the case (2) you can use section indexes (e.g. enum values) to select the right NSFetchedResultsController. – Vladimír Slavík Jul 02 '14 at 18:23
1

How are you displaying two views at one time? However you're doing it, I'd suggest the two-controller method, perhaps with a wrapping class that holds both and controls any interaction between them. That seems to me to fit the best with the MVC paradigm.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • Hi @Kevin. I'm displaying two views like this : `FirstUIViewSubclass *firstView = [[FirstUIViewSubclass alloc] initWithFrame:... [self.view addSubView:firstView] ...` and the same for the Second subclass – Dabrut Oct 20 '11 at 14:52