[IOS]非常不错的导航控制器的应用Demo

简介:

我们在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,如需转载请自行联系原作者

相关文章
|
29天前
|
设计模式 安全 Swift
探索iOS开发:打造你的第一个天气应用
【9月更文挑战第36天】在这篇文章中,我们将一起踏上iOS开发的旅程,从零开始构建一个简单的天气应用。文章将通过通俗易懂的语言,引导你理解iOS开发的基本概念,掌握Swift语言的核心语法,并逐步实现一个具有实际功能的天气应用。我们将遵循“学中做,做中学”的原则,让理论知识和实践操作紧密结合,确保学习过程既高效又有趣。无论你是编程新手还是希望拓展技能的开发者,这篇文章都将为你打开一扇通往iOS开发世界的大门。
|
30天前
|
搜索推荐 IDE API
打造个性化天气应用:iOS开发之旅
【9月更文挑战第35天】在这篇文章中,我们将一起踏上iOS开发的旅程,通过创建一个个性化的天气应用来探索Swift编程语言的魅力和iOS平台的强大功能。无论你是编程新手还是希望扩展你的技能集,这个项目都将为你提供实战经验,帮助你理解从构思到实现一个应用的全过程。让我们开始吧,构建你自己的天气应用,探索更多可能!
52 1
|
8天前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
30 5
|
2月前
|
存储 IDE 开发工具
移动应用开发之旅:打造你的首个iOS应用
【9月更文挑战第23天】在数字化浪潮中,移动应用已成为连接用户与数字世界的关键桥梁。本文将带领读者踏上开发属于自己的第一个iOS移动应用的旅程,从理解移动操作系统的核心概念出发,逐步深入到实际的应用构建过程中。通过简洁明了的语言和具体的代码示例,我们将一起探索如何在苹果的iOS平台上实现一个简单的“待办事项列表”应用,让读者不仅能够学习到编程知识,还能体会到将想法转化为现实产品的成就感。无论你是编程新手还是希望扩展技能的开发者,这篇文章都将为你提供一个实用的指南,帮助你迈出成为移动应用开发者的第一步。
|
2月前
|
开发框架 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的指南
在移动应用开发的广阔天地中,安卓与iOS两大平台各占半壁江山。本文将深入浅出地对比这两大操作系统的开发环境、工具和用户体验设计,揭示它们在编程语言、开发工具以及市场定位上的根本差异。我们将从开发者的视角出发,逐步剖析如何根据项目需求和目标受众选择适合的平台,同时探讨跨平台开发框架的利与弊,为那些立志于打造下一个热门应用的开发者提供一份实用的指南。
57 5
|
2月前
|
Swift iOS开发 UED
揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【9月更文挑战第5天】本文通过具体案例介绍如何在iOS应用中使用Swift与UIKit实现自定义按钮动画,当用户点击按钮时,按钮将从圆形变为椭圆形并从蓝色渐变到绿色,释放后恢复原状。文中详细展示了代码实现过程及动画平滑过渡的技巧,帮助读者提升应用的视觉体验与特色。
54 11
|
2月前
|
API iOS开发
探索iOS开发:打造你的第一个天气应用
【8月更文挑战第31天】 在这篇文章中,我们将一起潜入iOS开发的海洋,从初学者的角度出发,一步步构建我们自己的天气应用。通过实际的项目实践,你将学习到如何获取网络数据、如何在界面上展示这些数据,以及如何处理用户交互。文章以通俗易懂的语言,结合代码示例,引导你理解并实现一个简单天气应用的核心功能。无论你是编程新手还是希望扩展你的iOS开发技能,这篇文章都将为你提供宝贵的指导和启发。
|
2月前
|
开发工具 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的关键考量
在数字时代的浪潮中,安卓和iOS这两大操作系统如同双子星座般耀眼夺目,引领着移动应用的潮流。它们各自拥有独特的魅力和深厚的用户基础,为开发者提供了广阔的舞台。然而,正如每枚硬币都有两面,安卓与iOS在开发过程中也展现出了截然不同的特性。本文将深入剖析这两者在开发环境、编程语言、用户体验设计等方面的显著差异,并探讨如何根据目标受众和项目需求做出明智的选择。无论你是初涉移动应用开发的新手,还是寻求拓展技能边界的资深开发者,这篇文章都将为你提供宝贵的见解和实用的建议,帮助你在安卓与iOS的开发之路上更加从容自信地前行。
|
2月前
|
图形学 iOS开发 Android开发
从Unity开发到移动平台制胜攻略:全面解析iOS与Android应用发布流程,助你轻松掌握跨平台发布技巧,打造爆款手游不是梦——性能优化、广告集成与内购设置全包含
【8月更文挑战第31天】本书详细介绍了如何在Unity中设置项目以适应移动设备,涵盖性能优化、集成广告及内购功能等关键步骤。通过具体示例和代码片段,指导读者完成iOS和Android应用的打包与发布,确保应用顺利上线并获得成功。无论是性能调整还是平台特定的操作,本书均提供了全面的解决方案。
137 0
|
3月前
|
JSON API 定位技术
探索iOS开发之旅:打造你的第一个天气应用
【8月更文挑战第31天】 在这篇文章中,我们将一起踏上iOS开发的冒险旅程,学习如何从零开始构建一个简单的天气应用。通过实际操作和代码示例,你将了解到如何在Xcode中设置项目、使用Swift语言编写代码、以及接入第三方API来获取实时天气数据。无论你是刚入门的新手还是想扩展知识的开发者,这篇文章都将为你提供有价值的指导和灵感。