UITableView with Drag and Drop
Drag & Drop
UITableView has support to Drag and Drop order change.
You don’t need to implement by yourself.
Switch Edit mode
To enable UITableView order change, you need to switch editable mode.
[self.tableView setEditing:YES];
Example
This example uses just simple Table and button to change mode.
header(ViewController.h)
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @end
code
#import "ViewController.h" @interface ViewController () @property (nonatomic)UITableView *tableView; @property (nonatomic)UIButton *button; @property (nonatomic)NSMutableArray *items; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.view setBackgroundColor:[UIColor whiteColor]]; self.tableView = [[UITableView alloc] init]; self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 44); self.button = [[UIButton alloc] init]; self.button.frame = CGRectMake(0, self.view.bounds.size.height - 44, self.view.bounds.size.width, 44); [self.button setTitle:@"Edit" forState:UIControlStateNormal]; [self.button setBackgroundColor:[UIColor blueColor]]; [self.button addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside]; [self initTableItem]; [self.view addSubview:self.tableView]; [self.view addSubview:self.button]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.tableView reloadData]; } - (void)initTableItem { self.items = [NSMutableArray array]; [self.items addObject:@"A"]; [self.items addObject:@"Bo"]; [self.items addObject:@"Br"]; [self.items addObject:@"Ch"]; [self.items addObject:@"Co"]; [self.items addObject:@"E"]; [self.items addObject:@"Pa"]; [self.items addObject:@"Pe"]; [self.items addObject:@"U"]; [self.items addObject:@"V"]; } - (void)logItems { int index = 0; for ( NSString *str in self.items ) { NSLog(@"%d: %@", index++, str); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - UITableViewDataSource, UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.items count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if ( cell == nil ) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text = [self.items objectAtIndex:indexPath.row]; return cell; } #pragma mark - Edit Mode - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; // No Delete icon } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Can move cell return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { int origin = sourceIndexPath.row; // Original position int to = destinationIndexPath.row; // Destination position NSLog(@"Origin %d, To %d", origin, to); NSString *swap = [self.items objectAtIndex:origin]; // Item } #pragma mark - Event Action - (void)editClick:(id)sender { [self.tableView setEditing:!self.tableView.editing]; } @end