From 48692f5a2dfadf7b3020e31aad362faa916f919c Mon Sep 17 00:00:00 2001 From: Roman Efimov Date: Sat, 13 Apr 2013 17:42:45 -0500 Subject: [PATCH] Deletable items --- RETableViewManager/RETableViewItem.h | 2 + RETableViewManager/RETableViewManager.m | 44 ++++++++++++++ .../project.pbxproj | 6 ++ .../Controllers/EditingViewController.h | 16 +++++ .../Controllers/EditingViewController.m | 58 +++++++++++++++++++ .../Classes/Controllers/RootViewController.m | 6 ++ 6 files changed, 132 insertions(+) create mode 100644 RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.h create mode 100644 RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m diff --git a/RETableViewManager/RETableViewItem.h b/RETableViewManager/RETableViewItem.h index 6df2eee..8ad51a0 100644 --- a/RETableViewManager/RETableViewItem.h +++ b/RETableViewManager/RETableViewItem.h @@ -40,6 +40,8 @@ @property (strong, readwrite, nonatomic) UIView *accessoryView; @property (copy, readwrite, nonatomic) void (^selectionHandler)(id item); @property (copy, readwrite, nonatomic) void (^accessoryButtonTapHandler)(id item); +@property (assign, readwrite, nonatomic) BOOL deletable; +@property (copy, readwrite, nonatomic) void (^deletionHandler)(id item); @property (assign, readwrite, nonatomic) CGFloat cellHeight; + (id)item; diff --git a/RETableViewManager/RETableViewManager.m b/RETableViewManager/RETableViewManager.m index 0e5e121..2dab8bf 100644 --- a/RETableViewManager/RETableViewManager.m +++ b/RETableViewManager/RETableViewManager.m @@ -188,9 +188,53 @@ return UITableViewAutomaticDimension; } +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath +{ + +} + +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath +{ + return NO; +} + +- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath +{ + return proposedDestinationIndexPath; +} + +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; + RETableViewItem *item = [section.items objectAtIndex:indexPath.row]; + if ([item isKindOfClass:[RETableViewItem class]]) { + return item.deletable; + } + return NO; +} + +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + 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]; + } + } +} + #pragma mark - #pragma mark Table view delegate +- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath +{ + return UITableViewCellEditingStyleDelete; +} + - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { RETableViewSection *section = [_sections objectAtIndex:indexPath.section]; diff --git a/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj b/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj index d9ae7f1..2f2d828 100644 --- a/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj +++ b/RETableViewManagerExample/RETableViewManagerExample.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 30DB067816E934AD006C9530 /* RETableViewManager.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 30DB066516E934AD006C9530 /* RETableViewManager.bundle */; }; 30DB067916E934AD006C9530 /* RETableViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 30DB066716E934AD006C9530 /* RETableViewManager.m */; }; 30DB067A16E934AD006C9530 /* RETableViewSection.m in Sources */ = {isa = PBXBuildFile; fileRef = 30DB066916E934AD006C9530 /* RETableViewSection.m */; }; + 30E16A48171A103400F7C258 /* EditingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 30E16A47171A103300F7C258 /* EditingViewController.m */; }; 30EF93B316E039B800B84981 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30EF93B216E039B800B84981 /* UIKit.framework */; }; 30EF93B516E039B800B84981 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30EF93B416E039B800B84981 /* Foundation.framework */; }; 30EF93B716E039B800B84981 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30EF93B616E039B800B84981 /* CoreGraphics.framework */; }; @@ -106,6 +107,8 @@ 30DB066716E934AD006C9530 /* RETableViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RETableViewManager.m; sourceTree = ""; }; 30DB066816E934AD006C9530 /* RETableViewSection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RETableViewSection.h; sourceTree = ""; }; 30DB066916E934AD006C9530 /* RETableViewSection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RETableViewSection.m; sourceTree = ""; }; + 30E16A46171A103300F7C258 /* EditingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditingViewController.h; sourceTree = ""; }; + 30E16A47171A103300F7C258 /* EditingViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EditingViewController.m; sourceTree = ""; }; 30EF93AF16E039B800B84981 /* RETableViewManagerExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RETableViewManagerExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 30EF93B216E039B800B84981 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 30EF93B416E039B800B84981 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -163,6 +166,8 @@ 302D214C170B25B10085654F /* ListViewController.m */, 302D214D170B25B10085654F /* RootViewController.h */, 302D214E170B25B10085654F /* RootViewController.m */, + 30E16A46171A103300F7C258 /* EditingViewController.h */, + 30E16A47171A103300F7C258 /* EditingViewController.m */, ); path = Controllers; sourceTree = ""; @@ -431,6 +436,7 @@ 300E5B4217170B0200910013 /* RETableViewLongTextCell.m in Sources */, 30082D1A17170EDD008B0DB5 /* REPlaceholderTextView.m in Sources */, 30D208641718FFDE00144E46 /* REMultipleChoiceItem.m in Sources */, + 30E16A48171A103400F7C258 /* EditingViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.h b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.h new file mode 100644 index 0000000..076b9be --- /dev/null +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.h @@ -0,0 +1,16 @@ +// +// EditingViewController.h +// RETableViewManagerExample +// +// Created by Roman Efimov on 4/13/13. +// Copyright (c) 2013 Roman Efimov. All rights reserved. +// + +#import +#import "RETableViewManager.h" + +@interface EditingViewController : UITableViewController + +@property (strong, readonly, nonatomic) RETableViewManager *manager; + +@end diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m new file mode 100644 index 0000000..8372b47 --- /dev/null +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/EditingViewController.m @@ -0,0 +1,58 @@ +// +// EditingViewController.m +// RETableViewManagerExample +// +// Created by Roman Efimov on 4/13/13. +// Copyright (c) 2013 Roman Efimov. All rights reserved. +// + +#import "EditingViewController.h" + +@interface EditingViewController () + +@end + +@implementation EditingViewController + +- (void)viewDidLoad +{ + [super viewDidLoad]; + self.navigationItem.rightBarButtonItem = self.editButtonItem; + self.title = @"Editing"; + + //__typeof (&*self) __weak weakSelf = self; + + // Create manager + // + _manager = [[RETableViewManager alloc] init]; + _manager.style.textFieldPositionOffset = CGSizeMake(0, 0); + + // Set delegate and datasource + // + self.tableView.dataSource = _manager; + self.tableView.delegate = _manager; + + // Add sections and items + // + RETableViewSection *section = [[RETableViewSection alloc] initWithHeaderTitle:@"Section 1"]; + [_manager addSection:section]; + + for (NSInteger i = 1; i <= 10; i++) { + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + item.deletable = YES; + item.deletionHandler = ^(RETableViewItem *item) { + NSLog(@"Item removed: %@", item.title); + }; + [section addItem:item]; + } + + section = [[RETableViewSection alloc] initWithHeaderTitle:@"Section 2"]; + [_manager addSection:section]; + + for (NSInteger i = 1; i <= 10; i++) { + RETableViewItem *item = [RETableViewItem itemWithTitle:[NSString stringWithFormat:@"Item %i", i] accessoryType:UITableViewCellAccessoryNone selectionHandler:nil]; + [section addItem:item]; + } +} + +@end diff --git a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m index 3bfc891..ae1ea04 100644 --- a/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m +++ b/RETableViewManagerExample/RETableViewManagerExample/Classes/Controllers/RootViewController.m @@ -9,6 +9,7 @@ #import "RootViewController.h" #import "ControlsViewController.h" #import "ListViewController.h" +#import "EditingViewController.h" @interface RootViewController () @@ -46,6 +47,11 @@ [weakSelf.tableView deselectRowAtIndexPath:item.indexPath animated:YES]; [weakSelf.navigationController pushViewController:[[ListViewController alloc] initWithStyle:UITableViewStylePlain] animated:YES]; }]]; + + [section addItem:[RETableViewItem itemWithTitle:@"Editing" accessoryType:UITableViewCellAccessoryDisclosureIndicator selectionHandler:^(RETableViewItem *item) { + [weakSelf.tableView deselectRowAtIndexPath:item.indexPath animated:YES]; + [weakSelf.navigationController pushViewController:[[EditingViewController alloc] initWithStyle:UITableViewStyleGrouped] animated:YES]; + }]]; } @end