Overview
This example is based on an example of the last article that was modified, mainly to illustrate the use of the UITableView editing mode, including state changes, moving rows, deleting rows.
Run Results
Process Overview
See Code and comments, not difficult
Main codeh file
cityviewcontroller.h// nattab//// Created by God Lin on 14/12/7.// Copyright (c) 2014 Arbboter. All rights reserved.//#import <UIKit/UIKit.h> @interface Cityviewcontroller:uiviewcontroller < Uitabbardelegate, uitableviewdatasource>{ 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 uitableviewdelegate-( Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{return uitableviewcelleditingstyledelete;} #pragma uitableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return [Self._arrayname count];} Insert Delete-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{if (Editingstyle = = uitableviewcelleditingstyledelete) {[self._a Rrayname RemoveObjectAtIndex:indexPath.row]; [TableView Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation:uitablEviewrowanimationfade]; } else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {//Create a new instance of the appropriate C Lass, insert it into the array, and add a new row to the table view}}//move-(void) TableView: (UITableView *) TableView m Overowatindexpath: (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 Dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {CELl = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:cellidentifier] Autorelease ]; } nsstring* iconstr = [NSString stringwithformat:@ "Animal_%u.png", 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 a 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<nrow; i++) {str = [nsstring stringwithformat:@ '%@_%c%c%c%c ', Self.title, Arc4random ()%26+ ' a ' , Arc4random ()%26+ ' a ', arc4random ()%26+ ' a ', arc4random ()%26+ ' a ']; [Self._arrayname ADDOBJECT:STR]; }//Refresh data [Self._tableview reloaddata];} -(void) didreceivememorywarning{[_arrayname release]; [_tableview release]; [Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Engineering Code
Ios-uitableview Editing Mode Example