What is the design reason used by UITableView to complete and what are the benefits of configuring delegates and data sources?

advertisements

Whats design pattern does UITableView use to populate and what are the benefits? Is it delegate pattern? Reason I am asking is that it's not just delegate but the datasource as well.Seems more like along the line with MVC.

I have just gone through a couple of tutorials online their\my code is working but it looks like I am missing the point.I end with all these methods in my main controller.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;//any number based on datasource size.
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    cell.text = [names objectAtIndex:indexPath.row];//names is an array.
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

It is a view based application . Should it be in a sperate controller.Otherwise it just looks messy and over the top way of doing something simple. I am not at all saying Objective C or apple is wrong but just that I am a beginner and missing the whole point of this delegate and datasource setup.

so to summarise can someone please explain:

1-Whats the benefit of this delegate and datasource setup?
2-Whats the name of this design pattern?
3-Should I have a separate controller (in view based application)?


  1. In Apple's parlance, delegate implements call-back methods which modify the UI behavior, and dataSource provides the data. In a bigger app, you can use two different objects to be the delegate and the data source separately.

  2. I'm not familiar with the official terminology, sorry ...

  3. Depends on the size of your app. Even if you just use appDelegate for everything, it's recommended to add

     #pragma mark -- table view delegate methods
     ...methods...
     #pragma mark -- table view data source methods
     ...more methods...
    
    

    so that the method list is shown nicely inside Xcode.