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

Overview

The NIMutableCollectionViewModel class is a mutable collection view model.

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

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

The latter option is the recommended approach to adding new cells to a collection view. Each method in the mutable collection view model returns a data structure that can be used to inform the collection 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:[TestTextCollectionViewCellObject objectWithTitle:@"A cell"]];
// Inform the collection view that we've modified the model.
[self.collectionView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
Inheritance diagram for NIMutableCollectionViewModel:
NICollectionViewModel

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:
 
Creating Collection View Cells
id< NICollectionViewModelDelegatedelegate property
 
Creating Collection 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.

delegate

A delegate used to fetch collection view cells for the data source.

@property (nonatomic, weak) id<NICollectionViewModelDelegate> delegate;

initWithDelegate:

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

- (id)initWithDelegate:(id<NICollectionViewModelDelegate>)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 *)listArray delegate:(id<NICollectionViewModelDelegate>)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];
[[NICollectionViewModel 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<NICollectionViewModelDelegate>)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"],
[NICollectionViewModelFooter footerWithTitle:@"Footer"],
nil];
[[NICollectionViewModel 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.