Classes and files
Appdelegate.m
#import "AppDelegate.h" #import "MainViewController.h" @implementation AppDelegate- (BOOL) Application: (uiapplication *) Application didfinishlaunchingwithoptions: (NSDictionary *) launchoptions{ self.window = [[uiwindow alloc] initwithframe:[[ uiscreen mainscreen] bounds]]; // override point for Customization after application launch. self.window.backgroundcolor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; mainviewcontroller *mainvc = [[mainviewcontroller alloc] init]; uinavigationcontroller *navvc = [[ uinavigationcontroller alloc] initwithrootviewcontroller:mainvc]; Self.window.rootviewcontroller = navvc; &nbsP; [mainvc release]; [navvc release]; [_ Window release]; return yes;} - (void) dealloc{ [_window release]; [super Dealloc];} - (void) applicationwillresignactive: (uiapplication *) application{ // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or sms message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause Ongoing tasks, disable timers, and throttle down opengl es frame rates. games should use this method to pause the game.} - (void) Applicationdidenterbackground: (uiapplication *) application{ // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called Instead of applicationwillterminate: when the user quits.} - (void) Applicationwillenterforeground: (uiapplication *) application{ // called as part of the transition From the background to the inactive state; here you can undo many of the changes made on entering the background.} - (void) applicationdidbecomeactive: (uiapplication *) application{ // restart any tasks that were paused (or not yet started) while the application was inactive. if the application was Previously in the background, optionally refresh the user interface.} - (void) Applicationwillterminate: (uiapplication *) application{ // called when the application is about to terminate. save data if appropriate. see also&nbsP;applicationdidenterbackground:.} @end
MainViewController.h
#import <UIKit/UIKit.h> @interface Mainviewcontroller:uiviewcontroller@property (nonatomic, retain) Nsmutablearray *array; @end
Mainviewcontroller.m
#import "MainViewController.h" @interface MainViewController () <uitableviewdatasource UITableViewDelegate> @property (nonatomic , retain) uitableview *tableview; //property @end@implementation mainviewcontroller- (ID) initwithnibname: (nsstring *) NibNameOrNil bundle: (nsbundle *) nibbundleornil{ self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // Custom initialization self.array = [nsmutablearray arraywithobjects:@ "Zheng Haowen", @ "Wu Yue", @ "I", @ "Peng Yu", @ "Gao full", @ "Hu Han", @ "Chen Fei", @ "Zhaoqingqing", @ "Qinghai Pool", @ "Jinqingmin", @ "Xie Chrysanthemum", @ "Lu Junting", @ "Huangsun", @ "Ying Yingpeng", @ "Meng Chaoxu", @ "Dong", @ "Cheng Rong", @ "Zhang Camry", @ "Zhaolin", @ "Southland Forest", @ "Wang June", @ "Zhengerniu Hongyu", @ "LiuSuchh ", nil]; } return self;} - (void) dealloc{ [_tableview release]; [_array Release]; [super dealloc];} - (void) viewdidload{ [super viewdidload]; // do any additional setup after loading the view. self.title = @ "Contacts"; self.tableview = [[uitableview alloc] initwithframe:cgrectmake (0, 0, 320, 480) style:UITableViewStylePlain]; self.tableview.datasource = self; self.tableview.delegate = self; [self.view addsubview:self.tableview ]; [ self.tableview release]; //Open TableView Edit mode// & Nbsp; [tableview setediting:yes animated:yes]; //system provides an edit button self.navigationitem.rightbarbuttonitem = self.editbuttonitem;} - (BOOL) TableView: (uitableview *) Tableview caneditrowatindexpath: (nsindexpath *) Whether indexpath//{// //can be edited// if (indexpath.row == 0 ) {// return YES;// }// return NO;//}- (void) TableView: (uitableview *) tableview Moverowatindexpath: (nsindexpath *) Sourceindexpath toindexpath: (nsindexpath *) Destinationindexpath{ //1. Get to the data source you want to move nsstring *str = &NBSP;[[SELF.ARRAY&NBSP;OBJECTATINDEX:SOURCEINDEXPATH.ROW]&NBSP;RETAIN];&NBSP;&NBSP;&NBSP;&NBSP;//2. The data is removed from the original position &NBSP;&NBSP;&NBSP;&NBSP;[SELF.ARRAY&NBSP;REMOVEOBJECTATINDEX:&NBSP;SOURCEINDEXPATh.row]; //3. Put the data in the final position [self.array insertObject:str ATINDEX:DESTINATIONINDEXPATH.ROW];&NBSP;&NBSP;&NBSP;&NBSP;//4. Memory management [str release];} Click the Edit button and the system will call this method- (void) setediting: (BOOL) editing animated: (BOOL) animated{ [ Super setediting:editing animated:animated]; nslog (@ "edtiting:%d,animated:%d" , editing,animated); //Use the Edit button of the system change the editing state of the TableView [ Self.tableview setediting:editing animated:animated];} Change the cell's edit style (insert/delete)- (Uitableviewcelleditingstyle) TableView: (uitableview *) tableview Editingstyleforrowatindexpath: (nsindexpath *) indexpath{ return uitableviewcelleditingstyleinsert; }//When you click the Delete button, the system calls the Protocol method- (void) TableView: ( uitableview *) Tableview commiteditingstyle: (uitableviewcelleditingstyle) EditIngstyle forrowatindexpath: (nsindexpath *) indexpath{ //judgment (Delete/Add) of the test operation if (Uitableviewcelleditingstyledelete == editingstyle) { //before deleting a cell, be sure to delete the corresponding content in the data source [self.array removeobjectatindex:indexpath.row]; // But delete the time, write delete the corresponding cell parameter 1; indexpath array to be removed parameter 2: Animate when you want to delete row NSArray *array = [NSArray arrayWithObjects:indexPath, nil]; [tableView deleteRowsAtIndexPaths:array Withrowanimation:uitableviewrowanimationright]; } else if (Uitableviewcelleditingstyleinsert == editingstyle) { nsstring *name = @ "SSSs "; [self.array addobject:name]; nsarray *array = [nsarray arraywithobjects:indexpath, nil]; [tableView insertRowsAtIndexPaths:array withrowanimation:uitableviewrowanimationleft]; } }- ( Nsinteger) TableView: (uitableview *) Tableview numberofrowsinsection: (Nsinteger) section{ return [self.array count];} - (uitableviewcell *) TableView: (uitableview *) Tableview cellforrowatindexpath: (NSIndexPath *) indexpath{ static nsstring *str = @ "AAA"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (Cell == nil) { &nbsP; cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseIdentifier:str] autorelease]; } cell.textlabel.text = [self.array objectatindex: indexpath.row]; cell.detailtextlabel.text = @ "class name"; Return cell;} - (void) TableView: (uitableview *) Tableview didselectrowatindexpath: (nsindexpath *) Indexpath//{// nslog (@ "%@", [self.array objectatindex:indexpath.row]);//}//- (Nsinteger) Numberofsectionsintableview: (uitableview *) Tableview//{// return 5;//}//- (nsstring *) TableView: (uitableview *) Tableview titleforheaderinsection: ( Nsinteger) section//{// return [nsstring stringwithformat:@ "section:%d", section];//}//- (CGFLOAT) TableView: (uitableview *) Tableview heightforheaderinsection: (Nsinteger) section//{// return 25;//}- (void) didreceivememorywarning{ [super Didreceivememorywarning]; // dispose of any resources that can be recreated.} /* #pragma mark - navigation// in a storyboard-based application, you will often want to do a little preparation before navigation- (void) Prepareforsegue: (uistoryboardsegue *) Segue sender: (ID) sender{ // Get the new view controller using [segue Destinationviewcontroller]. // pass the selected object to the new view controller.} */@end
This article is from "Xiao Liu _blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1550984
UITableViewCell edit, move, add or delete