Click here to support Nimbus development and make a donation at www.pledgie.com !
An iOS framework whose growth is bounded by O(documentation).
Table View Models

Overview

Nimbus table view models make building table views remarkably easy.

Rather than implement the data source methods in each table view controller, you assign a model to self.tableView.dataSource and only think about row creation.

Nimbus table view models implement many of the standard table view data source methods, including methods for section titles, grouped rows, and section indices. By providing this functionality in one object, Nimbus provides much more efficient implementations than one-off implementations that might otherwise be copied from one controller to another.

Creating Generic Static Models

In order to use the Nimbus table view model you create a model, assign it to your table view's data source after the table view has been created, and implement the model delegate to create the table view cells. You can use the Nimbus cell factory to avoid implementing the model delegate.

Below is an example of creating a basic list model:

NSArray* tableContents =
 [NSArray arrayWithObjects:
  [NSDictionary dictionaryWithObject:@"Row 1" forKey:@"title"],
  [NSDictionary dictionaryWithObject:@"Row 2" forKey:@"title"],
  [NSDictionary dictionaryWithObject:@"Row 3" forKey:@"title"],
  nil];

_model = [[NITableViewModel alloc] initWithListArray:tableContents
                                            delegate:self];

Below is an example of creating a basic sectioned model:

NSArray* tableContents =
 [NSArray arrayWithObjects:
  @"Section Title"
  [NSDictionary dictionaryWithObject:@"Row 1" forKey:@"title"],
  [NSDictionary dictionaryWithObject:@"Row 2" forKey:@"title"],

  @"Section Title"
  [NSDictionary dictionaryWithObject:@"Row 3" forKey:@"title"],
  nil];

_model = [[NITableViewModel alloc] initWithSectionedArray:tableContents
                                                 delegate:self];

Both of the above examples would implement the model delegate like so:

- (UITableViewCell *)tableViewModel: (NITableViewModel *)tableViewModel
                   cellForTableView: (UITableView *)tableView
                        atIndexPath: (NSIndexPath *)indexPath
                         withObject: (id)object {
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"row"];

  if (nil == cell) {
    cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                   reuseIdentifier: @"row"]
            autorelease];
  }

  cell.textLabel.text = [object objectForKey:@"title"];

  return cell;
}

Creating Forms

Let's say you want to create a form for a user to enter their username and password. You can easily do this with Nimbus using the Nimbus cell factory and the Nimbus form elements from the table cell catalog.

NSArray* tableContents =
[NSArray arrayWithObjects:
 @"Sign In",
 [NITextInputFormElement textInputElementWithID:kUsernameField placeholderText:@"Username" value:nil],
 [NITextInputFormElement passwordInputElementWithID:kPasswordField placeholderText:@"Password" value:nil],
 nil];

_model = [[NITableViewModel alloc] initWithSectionedArray:tableContents
                                                 delegate:(id)[NICellFactory class]];

When the user then hits the button to sign in, you can grab the values from the model by using the elementWithID: category method added to NITableViewModel by the form support.

NSString* username = [[_model elementWithID:kUsernameField] value];
NSString* password = [[_model elementWithID:kPasswordField] value];

See example: Static Table Model Creation

Classes

class  NITableViewModel
 A non-mutable table view model that complies to the UITableViewDataSource protocol. More...
Generated for Nimbus by doxygen 1.7.4-20110629