IOS-UITableView edit mode example
Summary
This example is based on the previous article. It mainly describes how to use the UITableView editing mode, including changing the status, moving rows, and deleting rows.
Running result
Process Overview
It is not difficult to see the code and comments.
Main Code h file
/// CityViewController. h // NatTab /// Created by God Lin on 14/12/7. // Copyright (c) 2014 arbboter. All rights reserved. // # import
@ Interface CityViewController: UIViewController
{NSMutableArray * _ arrayName; UITableView * _ tableView;} @ property (nonatomic, retain) NSMutableArray * _ arrayName; @ property (nonatomic, retain) UITableView * _ tableView; @ end
M file
/// CityViewController. m // NatTab // Created by God Lin on 14/12/7. // Copyright (c) 2014 arbboter. all rights reserved. // # import "CityViewController. h "@ interface CityViewController () @ end @ implementation CityViewController @ synthesize _ arrayName; @ synthesize _ tableView; # pragma metadata-(Inline) tableView :( UITableView *) tableView metadata :( NSIndexPath *) indexPath {return UITableViewCellEditingStyleDelete;} # pragma UITableViewDataSource-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [self. _ arrayName count];} // insert and delete-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = callback) {[self. _ arrayName removeObjectAtIndex: indexPath. row]; [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: equals];} else if (editingStyle = equals) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view} // move-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {NSString * fromObj = [self. _ arrayName objectAtIndex: sourceIndexPath. row]; [self. _ arrayName insertObject: fromObj atIndex: destinationIndexPath. row]; [self. _ arrayName removeObjectAtIndex: sourceIndexPath. row];}-(BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath {return YES;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = nil; static NSString * CellIdentifier = @ "Cell"; cell = (UITableViewCell *) [tableView progress: CellIdentifier]; if (cell = nil) {cell = [[[UITableViewCell alloc] initWithStyle: Invalid reuseIdentifier: CellIdentifier] autorelease];} NSString * iconStr = [NSString stringWithFormat: @ "response ", arc4random () % 17 + 1]; cell. imageView. image = [UIImage imageNamed: iconStr]; cell. textLabel. text = [self. _ arrayName objectAtIndex: indexPath. row]; return cell;}-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1 ;}- (void) viewDidLoad {[super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self. clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. self. navigationItem. rightBarButtonItem = self. editButtonItem; CGRect viewRect = self. view. frame; self. _ tableView = [[UITableView alloc] initWithFrame: viewRect]; self. _ tableView. delegate = (id) self; self. _ tableView. dataSource = self; [self. view addSubview: self. _ tableView]; self. _ arrayName = [[NSMutableArray alloc] init];}-(void) setEditing :( BOOL) editing animated :( BOOL) animated {[super setEditing: editing animated: animated]; [self. _ tableView setEditing: editing animated: animated];}-(void) viewWillAppear :( BOOL) animated {int nRow = arc4random () % 50 + 20; NSString * str = nil; [self. _ arrayName removeAllObjects]; for (int I = 0; I
Engineering Code