UITableViewCell的编辑,移动,添加或者删除
类和文件
AppDelegate.m
#import"AppDelegate.h"#import"MainViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];MainViewController*mainVC=[[MainViewControlleralloc]init];UINavigationController*navVC=[[UINavigationControlleralloc]initWithRootViewController:mainVC];self.window.rootViewController=navVC;[mainVCrelease];[navVCrelease];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
MainViewController.h
#import<UIKit/UIKit.h>@interfaceMainViewController:UIViewController@property(nonatomic,retain)NSMutableArray*array;@end
MainViewController.m
#import"MainViewController.h"@interfaceMainViewController()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)UITableView*tableView;//属性@end@implementationMainViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitializationself.array=[NSMutableArrayarrayWithObjects:@"郑浩",@"吴月",@"杨雪",@"彭宇峰",@"高军全",@"胡寒予",@"陈腾飞",@"赵相庆",@"于青池",@"任庆民",@"谢菊花",@"吕俊廷",@"黄舜",@"翟英鹏",@"孟兆旭",@"王栋",@"卞成龙",@"张佳美",@"赵麟嵘",@"南国林",@"王俊",@"刘福彧",@"刘亚芳",nil];}returnself;}-(void)dealloc{[_tableViewrelease];[_arrayrelease];[superdealloc];}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.title=@"通讯录";self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStylePlain];self.tableView.dataSource=self;self.tableView.delegate=self;[self.viewaddSubview:self.tableView];[self.tableViewrelease];//开启tableView的编辑模式//[tableViewsetEditing:YESanimated:YES];//系统提供的一个编辑按钮self.navigationItem.rightBarButtonItem=self.editButtonItem;}//-(BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath//{////是否能够被编辑//if(indexPath.row==0){//returnYES;//}//returnNO;//}-(void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath{//1.获取到要移动的数据源NSString*str=[[self.arrayobjectAtIndex:sourceIndexPath.row]retain];//2.讲数据从原来的位置移除掉[self.arrayremoveObjectAtIndex:sourceIndexPath.row];//3.把数据放到最终的位置[self.arrayinsertObject:stratIndex:destinationIndexPath.row];//4.内存管理[strrelease];}//点击编辑按钮,系统会调用这个方法-(void)setEditing:(BOOL)editinganimated:(BOOL)animated{[supersetEditing:editinganimated:animated];NSLog(@"edtiting:%d,animated:%d",editing,animated);//利用系统的编辑按钮改变tableView的编辑状态[self.tableViewsetEditing:editinganimated:animated];}//改变cell的编辑样式(插入/删除)-(UITableViewCellEditingStyle)tableView:(UITableView*)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath*)indexPath{returnUITableViewCellEditingStyleInsert;}//点击delete按钮的时候,系统调用协议方法-(void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath{//对进行测操作的判断(删除/添加)if(UITableViewCellEditingStyleDelete==editingStyle){//在删除一个cell之前,一定要删除数据源里面相应的内容[self.arrayremoveObjectAtIndex:indexPath.row];//但是删除的时候,写删除相应cell参数1;要删除的indexPath组成的数组参数2:要删除row时候展现动画效果NSArray*array=[NSArrayarrayWithObjects:indexPath,nil];[tableViewdeleteRowsAtIndexPaths:arraywithRowAnimation:UITableViewRowAnimationRight];}elseif(UITableViewCellEditingStyleInsert==editingStyle){NSString*name=@"ssss";[self.arrayaddObject:name];NSArray*array=[NSArrayarrayWithObjects:indexPath,nil];[tableViewinsertRowsAtIndexPaths:arraywithRowAnimation:UITableViewRowAnimationLeft];}}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return[self.arraycount];}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*str=@"aaa";UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:str];if(cell==nil){cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]autorelease];}cell.textLabel.text=[self.arrayobjectAtIndex:indexPath.row];cell.detailTextLabel.text=@"全班人名";returncell;}//-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath//{//NSLog(@"%@",[self.arrayobjectAtIndex:indexPath.row]);//}//-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView//{//return5;//}//-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section//{//return[NSStringstringWithFormat:@"section:%d",section];//}//-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section//{//return25;//}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。