The iOS framework that grows only as fast as its documentation
NIMutableTableViewModel Class Reference

Overview

The NIMutableTableViewModel class is a mutable table view model.

When modifications are made to the model there are two ways to reflect the changes in the table view.

  • Call reloadData on the table view. This is the most destructive way to update the table view.
  • Call insert/delete/reload methods on the table view with the retuned index path arrays.

The latter option is the recommended approach to adding new cells to a table view. Each method in the mutable table view model returns a data structure that can be used to inform the table view of the exact modifications that have been made to the model.

Example of adding a new section:

// Appends a new section to the end of the model.
NSIndexSet* indexSet = [self.model addSectionWithTitle:@"New section"];
// Appends a cell to the last section in the model (in this case, the new section we just created).
[self.model addObject:[NITitleCellObject objectWithTitle:@"A cell"]];
// Inform the table view that we've modified the model.
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
Inheritance diagram for NIMutableTableViewModel:
NITableViewModel

Tasks

Modifying Objects
(NSArray *) - addObject:
 
(NSArray *) - addObject:toSection:
 
(NSArray *) - addObjectsFromArray:
 
(NSArray *) - insertObject:atRow:inSection:
 
(NSArray *) - removeObjectAtIndexPath:
 
Modifying Sections
(NSIndexSet *) - addSectionWithTitle:
 
(NSIndexSet *) - insertSectionWithTitle:atIndex:
 
(NSIndexSet *) - removeSectionAtIndex:
 
Updating the Section Index
(void) - updateSectionIndex
 
Configuration
NITableViewModelSectionIndex sectionIndexType property
 
BOOL sectionIndexShowsSearch property
 
BOOL sectionIndexShowsSummary property
 
(void) - setSectionIndexType:showsSearch:showsSummary:
 
Creating Table View Cells
NITableViewModelCellForIndexPathBlock createCellBlock property
 
Creating Table View Models
(id) - initWithDelegate:
 
(id) - initWithListArray:delegate:
 
(id) - initWithSectionedArray:delegate:
 
Accessing Objects
(id) - objectAtIndexPath:
 
(NSIndexPath *) - indexPathForObject:
 

Method Documentation

addObject:

Appends an object to the last section.

- (NSArray*)addObject:(id)object;
Discussion

If no sections exist, a section will be created without a title and the object will be added to this new section.

Parameters
objectThe object to append to the last section.
Returns
An array with a single NSIndexPath representing the index path of the new object in the model.

addObject:toSection:

Appends an object to the end of the given section.

- (NSArray*)addObject:(id)object toSection:(NSUInteger)section;
Discussion
Parameters
objectThe object to append to the section.
sectionThe index of the section to which this object should be appended.
Returns
An array with a single NSIndexPath representing the index path of the new object in the model.

addObjectsFromArray:

Appends an array of objects to the last section.

- (NSArray*)addObjectsFromArray:(NSArray *)array;
Discussion

If no section exists, a section will be created without a title and the objects will be added to this new section.

Parameters
arrayThe array of objects to append to the last section.
Returns
An array of NSIndexPath objects representing the index paths of the objects in the model.

insertObject:atRow:inSection:

Inserts an object into the given section at the given row.

- (NSArray*)insertObject:(id)object atRow:(NSUInteger)row inSection:(NSUInteger)section;
Discussion
Parameters
objectThe object to append to the section.
rowThe row within the section at which to insert the object.
sectionThe index of the section in which the object should be inserted.
Returns
An array with a single NSIndexPath representing the index path of the new object in the model.

removeObjectAtIndexPath:

Removes an object at the given index path.

- (NSArray*)removeObjectAtIndexPath:(NSIndexPath *)indexPath;
Discussion

If the index path does not represent a valid object then a debug assertion will fire and the method will return nil without removing any object.

Parameters
indexPathThe index path at which to remove a single object.
Returns
An array with a single NSIndexPath representing the index path of the object that was removed from the model, or nil if no object exists at the given index path.

addSectionWithTitle:

Appends a section with a given title to the model.

- (NSIndexSet*)addSectionWithTitle:(NSString *)title;
Discussion
Parameters
titleThe title of the new section.
Returns
An index set with a single index representing the index of the new section.

insertSectionWithTitle:atIndex:

Inserts a section with a given title to the model at the given index.

- (NSIndexSet*)insertSectionWithTitle:(NSString *)title atIndex:(NSUInteger)index;
Discussion
Parameters
titleThe title of the new section.
indexThe index in the model at which to add the new section.
Returns
An index set with a single index representing the index of the new section.

removeSectionAtIndex:

Removes a section at the given index.

- (NSIndexSet*)removeSectionAtIndex:(NSUInteger)index;
Discussion
Parameters
indexThe index in the model of the section to remove.
Returns
An index set with a single index representing the index of the removed section.

updateSectionIndex

Updates the section index with the current section index settings.

- (void)updateSectionIndex;
Discussion

This method should be called after modifying the model if a section index is being used.

sectionIndexType

The section index type.

@property (nonatomic, readonly) NITableViewModelSectionIndex sectionIndexType;
Discussion

You will likely use NITableViewModelSectionIndexAlphabetical in practice.

NITableViewModelSectionIndexNone by default.

sectionIndexShowsSearch

Whether or not the search symbol will be shown in the section index.

@property (nonatomic, readonly) BOOL sectionIndexShowsSearch;
Discussion

NO by default.

sectionIndexShowsSummary

Whether or not the summary symbol will be shown in the section index.

@property (nonatomic, readonly) BOOL sectionIndexShowsSummary;
Discussion

NO by default.

createCellBlock

A block used to create a UITableViewCell for a given object.

@property (nonatomic, copy) NITableViewModelCellForIndexPathBlock createCellBlock;

initWithDelegate:

Initializes a newly allocated static model with the given delegate and empty contents.

- (id)initWithDelegate:(id<NITableViewModelDelegate>)delegate;
Discussion

This method can be used to create an empty model.

initWithListArray:delegate:

Initializes a newly allocated static model with the contents of a list array.

- (id)initWithListArray:(NSArray *)sectionedArray delegate:(id<NITableViewModelDelegate>)delegate;
Discussion

A list array is a one-dimensional array that defines a flat list of rows. There will be no sectioning of contents in any way.

Example

NSArray* contents =
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObject:@"Row 1" forKey:@"title"],
[NSDictionary dictionaryWithObject:@"Row 2" forKey:@"title"],
[NSDictionary dictionaryWithObject:@"Row 3" forKey:@"title"],
nil];
[[NIStaticTableViewModel alloc] initWithListArray:contents delegate:self];

initWithSectionedArray:delegate:

Initializes a newly allocated static model with the contents of a sectioned array.

- (id)initWithSectionedArray:(NSArray *)sectionedArray delegate:(id<NITableViewModelDelegate>)delegate;
Discussion

A sectioned array is a one-dimensional array that defines a list of sections and each section's contents. Each NSString begins a new section and any other object defines a row for the current section.

Example

NSArray* contents =
[NSArray arrayWithObjects:
@"Section 1",
[NSDictionary dictionaryWithObject:@"Row 1" forKey:@"title"],
[NSDictionary dictionaryWithObject:@"Row 2" forKey:@"title"],
@"Section 2",
// This section is empty.
@"Section 3",
[NSDictionary dictionaryWithObject:@"Row 3" forKey:@"title"],
[NITableViewModelFooter footerWithTitle:@"Footer"],
nil];
[[NIStaticTableViewModel alloc] initWithSectionedArray:contents delegate:self];

objectAtIndexPath:

Returns the object at the given index path.

- (id)objectAtIndexPath:(NSIndexPath *)indexPath;
Discussion

If no object exists at the given index path (an invalid index path, for example) then nil will be returned.

indexPathForObject:

Returns the index path of the given object within the model.

- (NSIndexPath*)indexPathForObject:(id)object;
Discussion

If the model does not contain the object then nil will be returned.

setSectionIndexType:showsSearch:showsSummary:

Configures the model's section index properties.

- (void)setSectionIndexType:(NITableViewModelSectionIndex)sectionIndexType showsSearch:(BOOL)showsSearch showsSummary:(BOOL)showsSummary;
Discussion

Calling this method will compile the section index depending on the index type chosen.

Parameters
sectionIndexTypeThe type of section index to display.
showsSearchWhether or not to show the search icon at the top of the index.
showsSummaryWhether or not to show the summary icon at the bottom of the index.