#import <UIKit/UIKit.h> typedef enum _ACTION_TYPE{ TYPE_NONE, TYPE_REMOVE, TYPE_ADD, TYPE_SELECT, TYPE_MOVE }ActionType; @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>{ UITableView *_tableView; NSMutableArray *_titles; UIToolbar *_toolbar; ActionType _actionType; } @property(retain, nonatomic) IBOutlet UIToolbar *toolbar; - (IBAction)doRemove:(id)sender; - (IBAction)doAdd:(id)sender; - (IBAction)doSelect:(id)sender; - (IBAction)doMove:(id)sender; @end
// // ViewController.m // UITableViewTest // // Created by Peng Leon on 12/11/13. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "ViewController.h" @implementation ViewController @synthesize toolbar = _toolbar; - (void)dealloc { [_toolbar release]; [_titles release]; [_tableView release]; [super dealloc]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _actionType = TYPE_NONE; self.navigationItem.title = @"UITableView Demo"; CGRect rect = CGRectMake(0., self.navigationController.view.bounds.size.height - _toolbar.frame.size.height, _toolbar.frame.size.width, _toolbar.frame.size.height); NSLog(@"rect -> %@", NSStringFromCGRect(rect)); [_toolbar setFrame:rect]; [self.navigationController.view addSubview:_toolbar]; // UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithTitle:@"Remove" style:UIBarButtonItemStylePlain target:self action:@selector(doRemove:)] autorelease]; // self.navigationItem.leftBarButtonItem = item1; // // // UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(doAdd:)] autorelease]; // self.navigationItem.rightBarButtonItem = item2; _titles = [[NSMutableArray alloc] init]; [_titles addObjectsFromArray:[NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", nil]]; _tableView = [[UITableView alloc] initWithFrame:[self.navigationController.view bounds] style:UITableViewStylePlain]; //自定识别头及高度 _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _tableView.delegate = self; _tableView.dataSource = self; [_tableView setEditing:NO]; _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//设置分隔线 [self.view addSubview:_tableView]; } - (IBAction)doRemove:(id)sender { if (_actionType == TYPE_NONE) { _actionType = TYPE_REMOVE; [_tableView setEditing:YES animated:YES]; }else{ _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } } - (IBAction)doAdd:(id)sender { if (_actionType == TYPE_NONE) { _actionType = TYPE_ADD; [_tableView setEditing:YES animated:YES]; }else{ _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } } - (IBAction)doSelect:(id)sender { if (_actionType == TYPE_NONE) { _actionType = TYPE_SELECT; [_tableView setEditing:YES animated:YES]; }else{ _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } } - (IBAction)doMove:(id)sender { if (_actionType == TYPE_NONE) { _actionType = TYPE_MOVE; [_tableView setEditing:YES animated:YES]; }else{ _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } } #pragma mark - #pragma mark UITableViewDelegate, UITableViewDataSource Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_titles count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; NSString *identifier = @"cell"; cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; } cell.textLabel.text = [_titles objectAtIndex:[indexPath row]]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"did selected row -> %d", [indexPath row]); } // Editing - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call canEditRowAtIndexPath: function."); return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call commitEditingStyle: function -> %@", indexPath); switch (_actionType) { case TYPE_NONE:{ return; }break; case TYPE_ADD:{ NSString *title = [NSString stringWithFormat:@"A_%d", [indexPath row]]; NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1 inSection:[indexPath section]]; [_titles insertObject:title atIndex:[indexPath row] + 1]; [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight]; }break; case TYPE_REMOVE:{ [_titles removeObjectAtIndex:[indexPath row]]; [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; }break; case TYPE_SELECT:{ }break; case TYPE_MOVE:{ }break; } _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call editingStyleForRowAtIndexPath: function."); // UITableViewCellEditingStyleNone 移动操作 // UITableViewCellEditingStyleDelete 删除操作 // UITableViewCellEditingStyleInsert 添加操作 // UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert 多选操作 UITableViewCellEditingStyle style; switch (_actionType) { case TYPE_NONE: case TYPE_MOVE:{ style = UITableViewCellEditingStyleNone; }break; case TYPE_ADD:{ style = UITableViewCellEditingStyleInsert; }break; case TYPE_REMOVE:{ style = UITableViewCellEditingStyleDelete; }break; case TYPE_SELECT:{ style = UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete; }break; } return style; } - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call shouldIndentWhileEditingRowAtIndexPath: function."); return YES; } - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call willBeginEditingRowAtIndexPath: function."); } - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"call didEndEditingRowAtIndexPath: function."); } // Moving - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { NSLog(@"call moveRowAtIndexPath %@ -> %@ ", fromIndexPath, toIndexPath); NSUInteger fromRow = [fromIndexPath row]; NSUInteger toRow = [toIndexPath row]; id object = [[_titles objectAtIndex:fromRow] retain]; [_titles removeObjectAtIndex:fromRow]; [_titles insertObject:object atIndex:toRow]; [object release]; _actionType = TYPE_NONE; [_tableView setEditing:NO animated:YES]; } @end