iPhone MVC Question on table views and structuring custom class models

I have been reading a lot about MVC these days and I think I have my head around it but I would appreciate some advice and informed opinions on how to best approach my problem.

I have 3 questions really all related to the MVC design pattern.

  1. In many of the examples I have encountered people have used the contoller (say of a Table view) to populate an array with objects of a custom class (say Student.h/m). But shouldn't the Student class have methods that are called that would return an array of data for the variable in the controller? Isn't that how the MVC works? That the model holds the definition of the data and takes responsibility for reading and writing it?
  2. In many table view examples, in the various books I have read, they all say, "for convenience we are going to make the controller our delegate and data source for the table". I have yet to see an example where table view does not use the controller as a data source. How would you hook up a table view to a different data source?
  3. I have 2 model classes "mission" and "airfield". Each one of these needs data from a XML file in the cloud. Do I write the parser in the mission/airfield implementation files? Do I create a separate Parser object? Should these models retun data to the controller as an array?

Whilst I understand a lot of the theory a lot of the examples I find on the web seem to break a lot of the concepts I thought I understood.

Any explanations would be most welcome. The quality of responses on this site are amazing. Thanks in advance

-------------Problems Reply------------

You seem to already have a pretty good understanding of how MVC works, so I'll just add a few comments.

  1. The controller is responsible for feeding data to the view. There could be a million different states and scenarios that would affect what data to use and how, and it's not the model's job to figure that out. The model holds the data, and the controller handles the logic.
  2. If you need a different delegate or data source for your table view, you can instantiate them in the table view controller, and then tell the table view about them using the delegate and dataSource properties. They need to implement the required methods of the UITableViewDelegate and UITableViewDataSource protocols.
  3. Sometimes models and controllers overlap in functionality, and this is a perfect example of such a case.

    One solution could be to let the mission and airfield classes inherit from a custom class that knows how to download the required XML and set up a parser, so they just need to provide the URL and override the parser callbacks for their specific tags. Everything else related to downloading the XML could be handled in the super class.

    Another way could be to create a separate class that takes a URL and returns some XML, and then the mission and airfield classes call that method and parse the XML independently.

    The wrong way is to let both the mission and airfield classes know how to download and parse, because you'd have to maintain the code in both places.

    It's OK to have a data loading controller, that hands over the data to the view controller, so the controller code can be very specific (and maintainable).

Category:iphone Views:1 Time:2011-07-06

Related post

  • iphone navigation controler and table view 2010-06-23

    In my iphone app i have a button which changes the view (to another xib file). How can i have this xib file to contain a navigation controller with a back button (which is at the top) and the bottom part a table view that has a scrollbar? I assume th

  • iPhone - adjust size of table view according to text 2011-02-24

    Is there a way to adjust the size of a table view AND get the text to wrap in iPhone? I'm using the following code, and it succeeds in adjusting the height of the cell, but the text doesn't wrap, it just ends with "...." - (CGFloat)tableView:(UITable

  • Need Help Populating Table View With Structure Data 2011-12-23

    I'm trying to fill a table with data I have in a structure, elements_table, which contains an alpha literal element_name. The following does not give a syntax error, but a warning of an incompatible pointer type. I've never worked with a structure in

  • iphone - Problem loading a table view inside a viewcontroller 2011-08-02

    I have a TabBarController, which consists of three tabs, one of which is the settings tab. I am trying to sync the settings of the app with NSUserDefaults + settings.bundle and the in app settings that I have in the settings tab. When I change the se

  • iPhone Web services and table view 2011-12-02

    I have a table view that is getting it's data from a web service via an xml parser. I can get the data just fine on the first view now my problem is that when i select a row it should take me to a second with that has a table as well and i should sel

  • ASP.NET MVC 3 - Portable Area View doesn't find my model 2011-08-03

    I've started using MvcContrib's Portable Areas and everything works fine for the very simple views, but when I want to use a custom model in my view i get the error saying the namespace doesn't exist. The view is set to be embedded as resource. And i

  • How to decrease width of table view cells for iphone 2010-06-11

    I'm trying to create a grouped table view with two sections. For the first section I would like the width to be only half the screen. For the second section it would be the standard width. Also, next to the first section I would like to put a button.

  • Add different image as background in each section in grouped table view in iphone applications 2010-09-27

    I want to create an Iphone application using Grouped Table View. I created the Grouped Table View. My Grouped Table View having the three section. I want to add different images as background for each section in grouped table view. If i use the follo

  • Add view from table view cell 2011-06-30

    Ok I know this question has been asked before but mine is different. My app has an add button and edit button that deletes/adds table views. I want every cell that is created by the user to go to the same view. I've been looking everywhere for the co

  • Upon exiting UISearchDisplayController's search table view, the screen flashes 2009-10-26

    I am using UISearchDisplayController to implement the search feature on a table view. My table view cell uses custom background image. When you first type a letter into the search textfield, the search results start appearing. However, when I click c

  • Auto height for label or textview within table view cell 2010-06-11

    I have a problem in an iPhone application. Application has a table view controller with custom table view cells. Each cell has a Label (please correct me if I need to use text view etc). I am getting text dynamically from a web service call and I don

  • View Web Page After Clicking Cell in a Table View 2011-01-21

    I have prepared an iPhone app with a table view inside a tab bar. The app works great until I click on a cell in the table, expecting it to display a web page. Here's the first part of the code that displays the table view: - (void)createTeenData { N

  • Need to change the color of table view selection 2009-09-28

    I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me --------------Solutions------------- The best way to do this is like this: // Customize the appearance of table view cells. - (

  • Odd white stripe at the right of a table view 2011-04-22

    In my app with nav controller, I have a weird white line appearing at the right of my table view, when my table view cells are customed. They're loaded from a NIB. This NIB has the right dimensions (320 width), and the image for the table view cell b

  • buttons in a table header to load tdifferent table views 2011-04-26

    What's the best way for me to add 2 custom buttons in the header of my UITableView? On clicking either of those buttons, a different table View is loaded. Thanks for the help! --------------Solutions------------- Add the two buttons to the header, an

  • Table view cells lose their data when leaving view using navigation bar 2011-05-17

    I have a table view which has custom cells. My table view cell get populated through the values inserted in an SQLite database. The database values are shown in my table view cells, but the problem is, when I navigate from my table view to the previo

  • Images from URLs (RSS Reader) displaying in table view - problem 2011-05-28

    I'm having a problem with a table view and some custom cells. Here is my code: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); static NSString *MyIdentifier = @

  • Loading Custom Table-View Cells From Nib Files 2011-08-25

    I am currently working though an example in the apple documents but am having a little trouble finding some of the things they are talking about, in particular inside A Closer Look at Table-View Cells > Loading Custom Table-View Cells From Nib Fil

  • use table view or A view and three Subview or use labels 2011-10-21

    i am a new Programmer....so i wants to know... what i use (table view, or view and three subviews, or i use labels for it) for generate this view on button click.... i do everything programatically... CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.076 (s). 11 q(s)