我们在iPhone开发的过程中,估计UINavgationController是最最常用的控件之一吧,截下来我就用一个demo来举例导航控制器的应用。包含了tableview中增删查改的功能。
导航控制器的应用Demo
实现步骤:
1.创建一个Empty项目,命名为Navdemo。
2.创建一个根视图控制器,继承自UINavgationController,命名为FirstViewController。
FirstViewController.h:
#import <UIKit/UIKit.h> @interface FirstViewController : UITableViewController @property(nonatomic,retain) NSMutableArray *array; @end
FirstViewController.m:
#import "FirstViewController.h" #import "DXWDiscosureButtonViewController.h" #import "DXWCheckViewController.h" @interface FirstViewController () @end @implementation FirstViewController static NSString *CellIdentifier = @"Cell"; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { self.array = @[[[DXWDiscosureButtonViewController alloc] initWithStyle:UITableViewStylePlain],[[DXWCheckViewController alloc] initWithStyle:UITableViewStylePlain]]; self.title = @"First"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //注册 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return [self.array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //[tableView registerClass:[SecondViewController class] forCellReuseIdentifier:@"aa"]; //这个多一个最后的一个参数,必须要有上面一行注册过的才能这样用,不然的话就去掉最后一个参数 //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //没有注册需要创建 // if (cell == nil) { // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; // } int row = [indexPath row]; //当有多个view没有继承SecondVIewController的时候,可以这样写 //[((UITableViewController *)self.array[row] valueForKey:@"Title")]; cell.textLabel.text = ((DXWDiscosureButtonViewController *)self.array[row]).title; cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image; //有右边的>符号 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // DXWDiscosureButtonViewController *detailViewController = [[DXWDiscosureButtonViewController alloc] init]; //[detailViewController release]; int row = [indexPath row]; [self.navigationController pushViewController:self.array[row] animated:YES]; } @end
3.修改Delegate,设置根视图控制器
#import "DXWAppDelegate.h" #import "FirstViewController.h" //导入根视图控制器头文件 @implementation DXWAppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //创建首页 FirstViewController *first = [[FirstViewController alloc] initWithStyle:UITableViewStylePlain]; //创建一个导航栏,其中的首页是FirstViewController UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first]; //将导航栏作为根视图控制器 self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
4.创建所有子视图的一个基类控制器,命名为SecondViewController
SecondViewController.h:
#import <UIKit/UIKit.h> @interface SecondViewController : UITableViewController @property(nonatomic,retain)UIImage *image; @end
SecondViewController.m:
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController static NSString *cellRequestIdentifier = @"CellReuseIdentifier"; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { //self.title = @"SecondView"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //注册 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellRequestIdentifier]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } @end
5.创建二级子视图控制器,也就是上图所示的水果选项卡的内容控制器,命名为DXWDiscosureButtonController,继承自SecondViewController。
DXWDiscosureButtonController.h:
#import "SecondViewController.h" @interface DXWDiscosureButtonViewController : SecondViewController @property(nonatomic,retain) NSMutableArray *array; @end
DXWDiscosureButtonController.m:
#import "DXWDiscosureButtonViewController.h" #import "ThirdViewController.h" @interface DXWDiscosureButtonViewController () @end @implementation DXWDiscosureButtonViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.image = [UIImage imageNamed:@"disclosureButtonControllerIcon"]; self.title = @"水果"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.array = [[NSMutableArray alloc] initWithObjects:@"apple",@"orange",@"pitch",@"lenmon",@"balana", nil]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } int row = [indexPath row]; cell.textLabel.text = [self.array objectAtIndex:row]; // cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image; //有右边的>符号 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)dealloc { [self.array release]; [super dealloc]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath row]; UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"你选择了%@",self.array[row]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [a show]; } //右边的>符号响应事件 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { int row = [indexPath row]; // UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"你选择了%@",self.array[row]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; // [a show]; ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; thirdViewController.str = self.array[row]; [self.navigationController pushViewController:thirdViewController animated:YES]; [thirdViewController release]; } @end
6.创建第三级子视图控制器,也就是点击了某个水果选项卡的最右边的那个绿色按钮,会跳转到第三个视图,并且显示你所点击的水果名字,命名为ThirdViewController,继承自普通的ViewController,并且带有xib文件
ThirdViewController.h:
#import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @property (retain, nonatomic) IBOutlet UILabel *lblshow; @property (nonatomic,copy) NSString *str; @end
ThirdViewController.m:
#import "ThirdViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"ThirdViewController"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.lblshow.text = self.str; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_lblshow release]; [super dealloc]; } @end
7.创建根视图控制器的第二个选项卡的二级视图控制器,也就是选择的省市选项卡,命名为DXWCheckViewController,继承自tableViewController,不带xib文件
实现的效果是点击某行,这行显示mark
DXWCheckViewController.h:
#import "SecondViewController.h" @interface DXWCheckViewController : SecondViewController @property(nonatomic,retain) NSMutableArray *array; @property(nonatomic,assign) int selectedValue; @end
DXWCheckViewController.m:
#import "DXWCheckViewController.h" @interface DXWCheckViewController () @end @implementation DXWCheckViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.image = [UIImage imageNamed:@"checkmarkControllerIcon.png"]; self.title = @"省市"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.selectedValue = -1; self.array = [[NSMutableArray alloc] initWithObjects:@"北京",@"上海",@"重庆",@"天津",@"江苏",@"浙江",@"河北",@"湖南",@"河南",@"湖北", nil]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } int row = [indexPath row]; cell.textLabel.text = [self.array objectAtIndex:row]; // cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image; //有右边的>符号 //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; if (self.selectedValue == row) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath row]; //方法一:简洁明了的方法 //self.selectedValue = row; //[tableView reloadData]; //方法二:现货的以前的选中的cell,将其accessoryType设为None NSIndexPath *path = [NSIndexPath indexPathForRow:self.selectedValue inSection:0]; //将轩中行的accessoryType设为Checkmark UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:path]; oldCell.accessoryType = UITableViewCellAccessoryNone; //将selectedValue设为当前轩中行 self.selectedValue = row; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; } -(void)dealloc { [self.array release]; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
8.添加继承自SecondViewController的班级选项卡的二级视图控制器,命名为ButtonViewController
ButtonViewController.h:
#import "SecondViewController.h" @interface ButtonViewController : SecondViewController @property(retain,nonatomic) NSArray *array; @end
ButtonViewController.m:
#import "ButtonViewController.h" @interface ButtonViewController () @end @implementation ButtonViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.image = [UIImage imageNamed:@"rowControlsIcon"]; self.title = @"班级"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.array = [NSArray arrayWithObjects:@"一班",@"二班",@"三班",@"四班",@"五班",@"六班",@"七班",@"八班",@"九班",@"十班",@"十一班",@"十二班",@"十三班",@"十四班",nil]; } //行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; UIImage *image = [UIImage imageNamed:@"button_up"];//设置button的背景图片 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setBackgroundImage:image forState:UIControlStateNormal]; //button.frame = CGRectMake(0, 0, 150, 30); [button sizeToFit];//自适应图片的大小 [button setTitle:@"详细信息" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = button; } cell.accessoryView.tag = [indexPath row]; int row = [indexPath row]; cell.textLabel.text = self.array[row]; return cell; } -(void)buttonClick:(id)sender { //通过button的上一级可以拿到那个cell,在通过cell来拿到它所在的行数,然后可以获得相应的数据 UIButton *button = sender; UITableViewCell *cell = (UITableViewCell *)[button superview]; NSIndexPath *indexpath = [self.tableView indexPathForCell:cell]; int row = [indexpath row]; NSLog(@"%d班",row+1); UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"详细信息" message:[NSString stringWithFormat:@"%d班",row+1] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; } @end
9.创建可以移动的tableview效果,命名为MoveViewController,继承自SecondViewController,不带xib
操作:点击Edit按钮之后,可以点击每行右边的按钮进行拖动。
MoveViewController.h:
#import "SecondViewController.h" @interface MoveViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @end
MoveViewController.m:
#import "MoveViewController.h" @interface MoveViewController () @end @implementation MoveViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"Move"; self.image = [UIImage imageNamed:@"moveMeIcon"]; NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小张",@"小康",@"小刘",@"小墩",@"大墩"]; self.array = [arr mutableCopy]; } return self; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } int row = [indexPath row]; cell.textLabel.text = self.array[row]; return cell; } - (void)viewDidLoad { [super viewDidLoad]; //首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)]; //[button setTitle:@"move"]; self.navigationItem.rightBarButtonItem = button; } -(void)itemButtonClick:(id)sender { //设置可以编辑 [self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态 } //设置编译图标 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } //设置是否可以移动 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { id object= self.array`sourceIndexPath row`; [self.array removeObject:self.array`sourceIndexPath row`]; [self.array insertObject:object atIndex:[destinationIndexPath row]]; NSLog(@"%d",[self.array count]); } @end
10.创建一个带删除功能的tableViewController,同样继承自SecondViewController,不带xib
DelViewController.h:
#import "SecondViewController.h" @interface DelViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @end
DelViewController.m:
#import "DelViewController.h" @interface DelViewController () @end @implementation DelViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"Delete"; self.image = [UIImage imageNamed:@"deleteMeIcon"]; NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小张",@"小康",@"小刘",@"小墩",@"大墩"]; self.array = [arr mutableCopy]; } return self; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } int row = [indexPath row]; cell.textLabel.text = self.array[row]; return cell; } - (void)viewDidLoad { [super viewDidLoad]; //首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)]; //[button setTitle:@"move"]; self.navigationItem.rightBarButtonItem = button; } -(void)itemButtonClick:(id)sender { //设置可以编辑 [self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态 if (self.tableView.editing) { [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; } else { [self.navigationItem.rightBarButtonItem setTitle:@"Delete"]; } } //设置编译图标 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } //将数据从数组和tableview中删掉 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath row]; [self.array removeObjectAtIndex:row]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } //可以根据行来设置delete按钮位置上button的标题 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"我要删你"; } @end
11.创建一个继承自SecondViewController,具有添加功能的视图控制器,命名为AddViewController
AddViewController.h:
#import "SecondViewController.h" @interface AddViewController : SecondViewController<UIAlertViewDelegate> @property(retain,nonatomic)NSMutableArray *array; @property(retain,nonatomic)NSIndexPath *indexPath; @end
AddViewController.m:
#import "AddViewController.h" @interface AddViewController () @end @implementation AddViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"Add"; self.image = [UIImage imageNamed:@"deleteMeIcon"]; NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小张",@"小康",@"小刘",@"小墩",@"大墩"]; self.array = [arr mutableCopy]; } return self; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } int row = [indexPath row]; cell.textLabel.text = self.array[row]; return cell; } - (void)viewDidLoad { [super viewDidLoad]; //首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)]; //[button setTitle:@"move"]; self.navigationItem.rightBarButtonItem = button; } -(void)itemButtonClick:(id)sender { //设置可以编辑 [self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态 if (self.tableView.editing) { [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; } else { [self.navigationItem.rightBarButtonItem setTitle:@"Delete"]; } } //设置编译图标 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; } //实现UIAlertView代理协议 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { //获取AlertView中的数据 NSString *str = [alertView textFieldAtIndex:0].text; //将数据插入到array数组中(位置是点击的位置的下一行) [self.array insertObject:str atIndex:[self.indexPath row]+1]; //用点击位置的下一行创建一个新的NSIndexPath int newrow = [self.indexPath row]+1; NSIndexPath *index = [NSIndexPath indexPathForRow:newrow inSection:0]; //在tableView的这个NSIndexPath中插入数据 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation: UITableViewRowAnimationFade]; } //添加 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //获得点击的indexPath self.indexPath = indexPath; //创建一个UIAlertView,用来获得数据 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enterstring" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; //带有输入框的UIAlertView alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; //添加固定的内容 // [self.array insertObject:@"aaa" atIndex:[indexPath row]]; // int row = [indexPath row]; // [self.array removeObjectAtIndex:row]; // [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } @end
12.创建一个Person类,具有名字和年龄属性
Person.h:
#import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying> @property(copy,nonatomic) NSString *name; @property(assign,nonatomic) int age; @end
Person.m:
#import "Person.h" @implementation Person -(id)copyWithZone:(NSZone *)zone { Person *p = [[Person alloc] init] ; //拷贝函数不需要release,这里用autorelease会报错 p.name = [self.name copy]; p.age = self.age; return p; } -(NSString *)description { NSLog(@"%@,%d",self.name,self.age); } @end
13.创建继承自SecondViewcontroller的具有修改功能的tableViewController,命名为ChangeViewController,不带xib文件:
ChangeViewController.h:
#import "SecondViewController.h" #import "Person.h" //申明协议,修改自己页面中的内容 @protocol change <NSObject> //协议1 //-(void)changeData:(id)controller;//这里也可以是子视图的controller对象,然后想获取对象里面的任何值都可以 //协议2 -(void)changeData:(int)row Per:(Person *)p; @end @interface ChangeViewController : SecondViewController<change> @property(retain,nonatomic)NSMutableArray *array; @property(copy,nonatomic)Person *per; //-(void)changeData:(int)row; @end
ChangeViewController.m:
#import "ChangeViewController.h" #import "EditViewController.h" @interface ChangeViewController () @end @implementation ChangeViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"修改"; self.image = [UIImage imageNamed:@"detailEditIcon"]; self.array = [[NSMutableArray alloc] initWithCapacity:5]; for (int i=0; i<5; i++) { Person *p = [[Person alloc] init]; p.name = [NSString stringWithFormat:@"xiaoming%d",i]; p.age = 20+i; [self.array addObject:p]; } } return self; } - (void)viewDidLoad { [super viewDidLoad]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { int row = [indexPath row]; static NSString *identifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } Person *p = self.array[row]; cell.textLabel.text = p.name; cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",p.age]; return cell; } //实现遵循协议的方法 -(void)changeData:(int)row Per:(Person *)p { [self.array replaceObjectAtIndex:row withObject:p]; [self.tableView reloadData]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Person *p = self.array[indexPath.row]; NSLog(@"%@,%d",p.name,p.age); EditViewController *edit = [[EditViewController alloc] init]; edit.per = [p copy]; edit.row = indexPath.row; edit.delegate = self; [self.navigationController pushViewController:edit animated:YES]; } @end
14.创建可以保存修改值的三级子试图控制器,带有xib
EditViewController.h:
#import <UIKit/UIKit.h> #import "Person.h" #import "ChangeViewController.h" @interface EditViewController : UIViewController @property(copy,nonatomic)Person *per;//对象 @property (retain, nonatomic) IBOutlet UITextField *name; @property (retain, nonatomic) IBOutlet UITextField *age; @property(assign,nonatomic) int row; //位置 @property(nonatomic,retain)id<change>delegate; @end
EditViewController.m:
#import "EditViewController.h" @interface EditViewController () @end @implementation EditViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"信息修改"; } return self; } -(void)viewWillAppear:(BOOL)animated { self.name.text = self.per.name; self.age.text = [NSString stringWithFormat:@"%d",self.per.age]; } - (void)viewDidLoad { [super viewDidLoad]; //首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(itemLeftButtonClick:)]; self.navigationItem.leftBarButtonItem = button; UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(itemRightButtonClick:)]; self.navigationItem.rightBarButtonItem = button1; } //返回 -(void)itemLeftButtonClick:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } //保存 -(void)itemRightButtonClick:(id)sender { [self changedata]; [self.navigationController popViewControllerAnimated:YES]; } - (void)changedata { self.per.name = self.name.text; self.per.age = [self.age.text intValue]; if ([self.delegate respondsToSelector:@selector(changeData:Per:)]) { [self.delegate changeData:self.row Per:self.per]; } } - (void)dealloc { [_name release]; [_age release]; [super dealloc]; } @end
Demo源文件:
http://download.csdn.net/detail/s10141303/5999165
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/10170290
欢迎关注我的微博:http://weibo.com/u/2590571922
本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366426,如需转载请自行联系原作者