From 790c8f3b08fdcc5850e535cfd9633b048a931a9b Mon Sep 17 00:00:00 2001 From: Roman Efimov Date: Sat, 13 Apr 2013 18:25:15 -0500 Subject: [PATCH] Cell moving restrictions --- RETableViewManager/RETableViewItem.h | 2 ++ RETableViewManager/RETableViewManager.m | 17 ++++++++++++++- .../Controllers/EditingViewController.m | 21 ++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/RETableViewManager/RETableViewItem.h b/RETableViewManager/RETableViewItem.h index 3ef23af..dc3935f 100644 --- a/RETableViewManager/RETableViewItem.h +++ b/RETableViewManager/RETableViewItem.h @@ -43,6 +43,8 @@ @property (assign, readwrite, nonatomic) BOOL deletable; @property (assign, readwrite, nonatomic) BOOL movable; @property (copy, readwrite, nonatomic) void (^deletionHandler)(id item); +@property (copy, readwrite, nonatomic) void (^moveHandler)(id item, NSIndexPath *sourceIndexPath, NSIndexPath *destinationIndexPath); +@property (copy, readwrite, nonatomic) BOOL (^allowNewIndexPath)(NSIndexPath *newIndexPath); @property (assign, readwrite, nonatomic) CGFloat cellHeight; + (id)item; diff --git a/RETableViewManager/RETableViewManager.m b/RETableViewManager/RETableViewManager.m index e515ad4..bf76c12 100644 --- a/RETableViewManager/RETableViewManager.m +++ b/RETableViewManager/RETableViewManager.m @@ -190,7 +190,15 @@ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { - + RETableViewSection *sourceSection = [_sections objectAtIndex:sourceIndexPath.section]; + RETableViewItem *item = [sourceSection.items objectAtIndex:sourceIndexPath.row]; + [sourceSection removeItemAtIndex:sourceIndexPath.row]; + + RETableViewSection *destinationSection = [_sections objectAtIndex:destinationIndexPath.section]; + [destinationSection insertItem:item atIndex:destinationIndexPath.row]; + + if (item.moveHandler) + item.moveHandler(item, sourceIndexPath, destinationIndexPath); } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath @@ -202,6 +210,13 @@ - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { + RETableViewSection *sourceSection = [_sections objectAtIndex:sourceIndexPath.section]; + RETableViewItem *item = [sourceSection.items objectAtIndex:sourceIndexPath.row]; + if (item.allowNewIndexPath) { + BOOL allowed = item.allowNewIndexPath(proposedDestinationIndexPath); + if (!allowed) + return sourceIndexPath; + } return proposedDestinationIndexPath; } diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m index 170e439..9c2abe7 100644 --- a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m @@ -38,7 +38,7 @@ [_manager addSection:section]; for (NSInteger i = 1; i <= 5; i++) { - RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Section 1, Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; item.deletable = YES; item.deletionHandler = ^(RETableViewItem *item) { NSLog(@"Item removed: %@", item.title); @@ -50,8 +50,11 @@ [_manager addSection:section]; for (NSInteger i = 1; i <= 5; i++) { - RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Section 2, Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; item.movable = YES; + item.moveHandler = ^(RETableViewItem *item, NSIndexPath *sourceIndexPath, NSIndexPath *destinationIndexPath) { + NSLog(@"Moved item: %@ from [%i,%i] to [%i,%i]", item.title, sourceIndexPath.section, sourceIndexPath.row, destinationIndexPath.section, destinationIndexPath.row); + }; [section addItem:item]; } @@ -59,11 +62,23 @@ [_manager addSection:section]; for (NSInteger i = 1; i <= 5; i++) { - RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Section 3, Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; item.movable = YES; item.deletable = YES; [section addItem:item]; } + + section = [[RETableViewSection alloc] initWithHeaderTitle:@"Can move only within section"]; + [_manager addSection:section]; + + for (NSInteger i = 1; i <= 5; i++) { + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Section 4, Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + item.movable = YES; + item.allowNewIndexPath = ^BOOL(NSIndexPath *newIndexPath) { + return (newIndexPath.section == section.index); + }; + [section addItem:item]; + } } @end