Skip to content

Commit

Permalink
Merge pull request jverkoey#306 from francisli/submissions
Browse files Browse the repository at this point in the history
Adds support for row re-ordering in NIMutableTableViewModel
  • Loading branch information
jverkoey committed Nov 13, 2012
2 parents de89bec + 35bb647 commit 00c6151
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/models/src/NIMutableTableViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@
atIndexPath:(NSIndexPath *)indexPath
inTableView:(UITableView *)tableView;

/**
* Asks the receiver whether the object at the given index path should be moveable.
*
* If this method is not implemented, the default response is assumed to be NO.
*/
- (BOOL)tableViewModel:(NIMutableTableViewModel *)tableViewModel
canMoveObject:(id)object
atIndexPath:(NSIndexPath *)indexPath
inTableView:(UITableView *)tableView;

/**
* Asks the receiver whether the given object should be moved.
*
* If this method is not implemented, the default response is assumed to be YES.
*
* Returning NO will stop the model from handling the move logic.
*/
- (BOOL)tableViewModel:(NIMutableTableViewModel *)tableViewModel
shouldMoveObject:(id)object
atIndexPath:(NSIndexPath *)indexPath
toIndexPath:(NSIndexPath *)toIndexPath
inTableView:(UITableView *)tableView;

/**
* Asks the receiver what animation should be used when deleting the object at the given index path.
*
Expand Down
26 changes: 26 additions & 0 deletions src/models/src/NIMutableTableViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(tableViewModel:canMoveObject:atIndexPath:inTableView:)]) {
id object = [self objectAtIndexPath:indexPath];
return [self.delegate tableViewModel:self canMoveObject:object atIndexPath:indexPath inTableView:tableView];
} else {
return NO;
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id object = [self objectAtIndexPath:sourceIndexPath];
BOOL shouldMove = YES;
if ([self.delegate respondsToSelector:@selector(tableViewModel:shouldMoveObject:atIndexPath:toIndexPath:inTableView:)]) {
shouldMove = [self.delegate tableViewModel:self shouldMoveObject:object atIndexPath:sourceIndexPath toIndexPath:destinationIndexPath inTableView:tableView];
}
if (shouldMove) {
id object = [self objectAtIndexPath:sourceIndexPath];
[self removeObjectAtIndexPath:sourceIndexPath];
[self insertObject:object atRow:destinationIndexPath.row inSection:destinationIndexPath.section];
}
}

@end


Expand Down

0 comments on commit 00c6151

Please sign in to comment.