diff --git a/RETableViewManager/RETableViewItem.h b/RETableViewManager/RETableViewItem.h index 8ad51a0..3ef23af 100644 --- a/RETableViewManager/RETableViewItem.h +++ b/RETableViewManager/RETableViewItem.h @@ -41,6 +41,7 @@ @property (copy, readwrite, nonatomic) void (^selectionHandler)(id item); @property (copy, readwrite, nonatomic) void (^accessoryButtonTapHandler)(id item); @property (assign, readwrite, nonatomic) BOOL deletable; +@property (assign, readwrite, nonatomic) BOOL movable; @property (copy, readwrite, nonatomic) void (^deletionHandler)(id item); @property (assign, readwrite, nonatomic) CGFloat cellHeight; diff --git a/RETableViewManager/RETableViewManager.m b/RETableViewManager/RETableViewManager.m index 2dab8bf..e515ad4 100644 --- a/RETableViewManager/RETableViewManager.m +++ b/RETableViewManager/RETableViewManager.m @@ -195,7 +195,9 @@ - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { - return NO; + RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; + RETableViewItem *item = [section.items objectAtIndex:indexPath.row]; + return item.movable; } - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath @@ -208,7 +210,7 @@ RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; RETableViewItem *item = [section.items objectAtIndex:indexPath.row]; if ([item isKindOfClass:[RETableViewItem class]]) { - return item.deletable; + return item.deletable || item.movable; } return NO; } @@ -218,12 +220,10 @@ if (editingStyle == UITableViewCellEditingStyleDelete) { RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; RETableViewItem *item = [section.items objectAtIndex:indexPath.row]; - if (item.deletable) { - if (item.deletionHandler) - item.deletionHandler(item); - [section.items removeObjectAtIndex:indexPath.row]; - [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; - } + if (item.deletionHandler) + item.deletionHandler(item); + [section.items removeObjectAtIndex:indexPath.row]; + [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } } @@ -232,7 +232,13 @@ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { - return UITableViewCellEditingStyleDelete; + RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; + RETableViewItem *item = [section.items objectAtIndex:indexPath.row]; + + if (item.deletable) + return UITableViewCellEditingStyleDelete; + + return UITableViewCellEditingStyleNone; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m index 8372b47..170e439 100644 --- a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m @@ -34,10 +34,10 @@ // Add sections and items // - RETableViewSection *section = [[RETableViewSection alloc] initWithHeaderTitle:@"Section 1"]; + RETableViewSection *section = [[RETableViewSection alloc] initWithHeaderTitle:@"Deletable"]; [_manager addSection:section]; - for (NSInteger i = 1; i <= 10; i++) { + for (NSInteger i = 1; i <= 5; i++) { RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; item.deletable = YES; item.deletionHandler = ^(RETableViewItem *item) { @@ -46,11 +46,22 @@ [section addItem:item]; } - section = [[RETableViewSection alloc] initWithHeaderTitle:@"Section 2"]; + section = [[RETableViewSection alloc] initWithHeaderTitle:@"Movable"]; [_manager addSection:section]; - for (NSInteger i = 1; i <= 10; i++) { + for (NSInteger i = 1; i <= 5; i++) { RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + item.movable = YES; + [section addItem:item]; + } + + section = [[RETableViewSection alloc] initWithHeaderTitle:@"Deletable & Movable"]; + [_manager addSection:section]; + + for (NSInteger i = 1; i <= 5; i++) { + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + item.movable = YES; + item.deletable = YES; [section addItem:item]; } }