[IOS]非常不错的导航控制器的应用Demo
2023-09-27 14:29:23 时间
我们在iPhone开发的过程中,估计UINavgationController是最最常用的控件之一吧,截下来我就用一个demo来举例导航控制器的应用。包含了tableview中增删查改的功能。
导航控制器的应用Demo 实现步骤: 1.创建一个Empty项目,命名为Navdemo。 2.创建一个根视图控制器,继承自UINavgationController,命名为FirstViewController。#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 () @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 () @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
5.创建二级子视图控制器,也就是上图所示的水果选项卡的内容控制器,命名为DXWDiscosureButtonController,继承自SecondViewController。
#import "SecondViewController.h" @interface DXWDiscosureButtonViewController : SecondViewController @property(nonatomic,retain) NSMutableArray *array; @end
DXWDiscosureButtonController.m:
#import "DXWDiscosureButtonViewController.h" #import "ThirdViewController.h" @interface DXWDiscosureButtonViewController () @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文件
#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 () @implementation ThirdViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"ThirdViewController"; return self;
@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 () @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
#import "SecondViewController.h" @interface ButtonViewController : SecondViewController @property(retain,nonatomic) NSArray *array; @end
ButtonViewController.m:
[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];
9.创建可以移动的tableview效果,命名为MoveViewController,继承自SecondViewController,不带xib
操作:点击Edit按钮之后,可以点击每行右边的按钮进行拖动。 MoveViewController.h:
#import "SecondViewController.h" @interface MoveViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @endMoveViewController.m:
#import "MoveViewController.h" @interface MoveViewController () @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
#import "SecondViewController.h" @interface DelViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @end
DelViewController.m:
#import "DelViewController.h" @interface DelViewController () @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;
//首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)]; //[button setTitle:@"move"]; self.navigationItem.rightBarButtonItem = button;
//设置可以编辑 [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 () @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;
//首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)]; //[button setTitle:@"move"]; self.navigationItem.rightBarButtonItem = button;
//设置可以编辑 [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];
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文件:
#import "SecondViewController.h" #import "Person.h" //申明协议,修改自己页面中的内容 @protocol change NSObject //协议1 //-(void)changeData:(id)controller;//这里也可以是子视图的controller对象,然后想获取对象里面的任何值都可以 //协议2 -(void)changeData:(int)row Per:(Person *)p; @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 () @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 i++) { Person *p = [[Person alloc] init]; p.name = [NSString stringWithFormat:@"xiaoming%d",i]; p.age = 20+i; [self.array addObject:p];
-(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)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
#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 () @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];
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
欢迎关注我的微博:http://weibo.com/u/2590571922
iOS小技能:应用外导航 1. 应用外导航:是指以URL跳转的方式(在iOS中就是以URL Scheme的方式),直接跳到对应的地图APP中,直接利用对方的功能来导航。这样的优点,一是接入方便,二是不增加自己APP的开销。 2. 应用内导航:是指使用地图服务提供的SDK(如高德,百度),直接将导航功能嵌入到我们自己的APP内部。 需求:跳转到已经安装的地图 背景:为了减少app内存开支,以URI跳转的方式直接跳转到对应的地图进行导航,让最专业的人做最专业的事。
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
相关文章
- Zscaler:iOS应用泄露的用户数据比Android更多
- IOS APP配置.plist汇总(转自coolweather )
- iOS KVC & KVO
- [转] Google 开源 iOS 应用测试工具:EarlGrey
- 构建iOS稳定应用架构时方案选择的思考,主要涉及工程结构,数据流思想和代码规范
- Splunk机器数据分析进军iOS移动应用领域
- iOS利用storyboard改动UITextField的placeholder文字颜色
- iOS - 深拷贝、浅拷贝探索验证
- iOS中 最新支付宝支付(AliPay) 韩俊强的博客
- iOS 应用测试
- iOS应用管理(优化)
- iOS应用代码段瘦身办法
- iOS开发UI篇—Quartz2D使用(图片剪切)
- 李洪强iOS开发之-Swift_00_介绍
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发之集成友盟应用统计
- IOS 教程:自定义链接下载应用并直接安装ipa-itms-services
- IOS应用APP上线流程
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证