views:

58

answers:

3

Hi All,

I am learning iPhone programming. I am starting with a simple example of displaying recently used documents in a UITableView.

What I am confused about is why do I need to have several classes and why cant I just use one?

Example, My class is called RecentFileList.

I need to implement controller, delegate and datasource as well as the actual table view. Can't I just do this all in my RecentFileList Class versus having to create a RecentFileListDelegate RecentFileListController, RecentFileListDataSource class, etc, etc.

Related to this can one define a class like:

@interface FileListView : NSObject <UITableViewDelegate> <UITableViewController> <UITableViewDataSource
{
    // code
}
@end

or would I just do the work to make my class a delegate and controller in init()?

A: 

If you want to display a UITableView you will almost always want to have a UIViewController object which manages the view and is also the data source and delegate of the table view. Commonly this class will derive from UITableViewController (which provides you some boilerplate code).

The view on the other hand can never be the same class, since for implementing a view, you would need to derive from UIView. But if you would like to create the view in your controller, this is again a common pattern and there exists a method loadView: where you can initialize your UI.

For starters I would recommend to you that you read the iPhone Programming Guide by Apple as well as a beginners tutorial (just google, there are tons) which will teach you the basics of the MVC pattern used heavily in Cocoa/CocoaTouch.

frenetisch applaudierend
+1  A: 

It's common practice to implement a full screen tableview's controller, datasource and delegate all in a single UITableViewController subclass. That is how all the Apple template code is set up.

However, you have not done this. This code:

@interface FileListView : NSObject <UITableViewDelegate> <UITableViewController> <UITableViewDataSource> 

... doesn't properly define the tableview controller. UITableViewController is a class not a protocol. It should look like:

@interface FileListView : UITableViewController <UITableViewDelegate,UITableViewDataSource>

To answer the question of why have the capability to have separate objects handle all three job: Well, sometimes you need that. Sometimes you have views with multiple tables. Sometimes you need to swap out the delegate or datasource.

Having multiple objects gives you flexibility and makes the code more modular. This makes it easier to troubleshoot, maintain and reuse.

TechZen
A: 

Cocoa is packed with Design Pattern, that might inspire fear at first, but are really elegant. When you are trying to find your way into a new framework, some things are cumbersome, but then, when you gain some experience, and start to produce real code, they will save a lot of time. Now you think that the it is rather unusual to have a table view controller here, a delegate there, and a datasource over there. And you are right, for simple apps, it might be too much. A few weeks from now you discover a wonderful new way to feed data into your table view, and that's good (and that's something that we all experience), and you will not have to touch your table view controller, nor the delegate, all you have to do is replace the datasource. You have save a lot of time. Delegates? Why bother? Instead of subclass the entire table view controller to perform some task when the user select a given cell, you can set a delegate. Again you save some time. At the end of the day, it is always a good idea to decouple the objects on your code.

ariel