The iOS framework that grows only as fast as its documentation
Table View Models

Classes

class  NIMutableCollectionViewModel
 The NIMutableCollectionViewModel class is a mutable collection view model. More...
 
protocol  <NIMutableTableViewModelDelegate>
 A protocol for NIMutableTableViewModel to handle editing states for objects. More...
 
class  NIMutableTableViewModel
 The NIMutableTableViewModel class is a mutable table view model. More...
 
class  NITableViewModel
 A non-mutable table view model that complies to the UITableViewDataSource protocol. More...
 
protocol  <NITableViewModelDelegate>
 A protocol for NITableViewModel to fetch rows to be displayed for the table view. More...
 

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:
[NITitleCellObject objectWithTitle:@"Row 1"],
[NITitleCellObject objectWithTitle:@"Row 2"],
[NITitleCellObject objectWithTitle:@"Row 3"],
nil];
_model = [[NITableViewModel alloc] initWithListArray:tableContents
delegate:self];

Below is an example of creating a basic sectioned model:

NSArray* tableContents =
[NSArray arrayWithObjects:
@"Section Title",
[NITitleCellObject objectWithTitle:@"Row 1"],
[NITitleCellObject objectWithTitle:@"Row 2"],
@"Section Title",
[NITitleCellObject objectWithTitle:@"Row 3"],
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