亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[IOS]非常不錯的導航控制器的應用Demo

發布時間:2020-07-13 13:32:19 來源:網絡 閱讀:334 作者:蓬萊仙羽 欄目:移動開發

我們在iPhone開發的過程中,估計UINavgationController是最最常用的控件之一吧,截下來我就用一個demo來舉例導航控制器的應用。包含了tableview中增刪查改的功能。

導航控制器的應用Demo

實現步驟:

1.創建一個Empty項目,命名為Navdemo。

2.創建一個根視圖控制器,繼承自UINavgationController,命名為FirstViewController。

[IOS]非常不錯的導航控制器的應用Demo

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。

[IOS]非常不錯的導航控制器的應用Demo

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文件

[IOS]非常不錯的導航控制器的應用Demo

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文件

[IOS]非常不錯的導航控制器的應用Demo

實現的效果是點擊某行,這行顯示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

[IOS]非常不錯的導航控制器的應用Demo

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

[IOS]非常不錯的導航控制器的應用Demo[IOS]非常不錯的導航控制器的應用Demo[IOS]非常不錯的導航控制器的應用Demo

操作:點擊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

[IOS]非常不錯的導航控制器的應用Demo

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

[IOS]非常不錯的導航控制器的應用Demo[IOS]非常不錯的導航控制器的應用Demo

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文件:

[IOS]非常不錯的導航控制器的應用Demo

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

[IOS]非常不錯的導航控制器的應用Demo[IOS]非常不錯的導航控制器的應用Demo[IOS]非常不錯的導航控制器的應用Demo

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
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

青岛市| 昭苏县| 河南省| 竹北市| 行唐县| 嘉定区| 柏乡县| 昭苏县| 东方市| 津市市| 辽源市| 宁夏| 甘孜| 武宣县| 公主岭市| 平定县| 盐边县| 饶阳县| 大化| 招远市| 濮阳县| 大理市| 大名县| 涟源市| 宾阳县| 江油市| 宁乡县| 石门县| 荔波县| 富宁县| 西贡区| 禹州市| 双城市| 青冈县| 佛学| 龙里县| 凤翔县| 海南省| 保山市| 易门县| 登封市|