環境:xcode6,iphone 4s simulator with iOS8.0xcode
1、功能界面介紹app
1.1 應用啓動進入聯繫人列表頁面,數據爲模擬數據,來源與一個plist文件:iphone
1.2 點擊右上角當+按鈕時,進入添加頁面,頁面跳轉採用UINavigationController,該頁面上左上角當取消按鈕直接導航至起始頁面,完成則添加數據至表格:ide
1.3 當點擊一行時,把數據設置至添加頁面,用戶可編輯(後期可優化爲點擊一行進入詳情頁面,再詳情頁面點擊編輯再進入編輯模式):工具
2、功能實現優化
2.1 首先創建一個Empty Application,創建完成後生成文件以下:atom
1. 再新建一個主view,用於應用加載時顯示:選擇cocoa touch class,命名爲RootViewController,父類爲UIViewController,勾選also create xib file。spa
說明:可選擇生產的xib,在右邊面板屬性的第一個選項取消use auto layout關閉自動調整,再選擇第四個選項卡設置size屬性配置顯示尺寸。設計
2.往界面中拖入一個tableview,xy設置0,大小設置於屏幕大小一致,打開雙屏模式(右上角工具欄第二個),按住ctrl, 選擇剛纔的tableview拖入code
RootViewController.h文件中,使得控制器擁有該tableview以便於控制:
// // RootViewController.h // ContactsView // // Created by Mike on 14-12-14. // Copyright (c) 2014年 Mike. All rights reserved. // #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @property (weak, nonatomic) IBOutlet UITableView *tableView; @end
相似方法建立一個用於用戶添加和編輯的界面 PersonInputViewController
3. 修改AppDelegate.m,設置應用窗口根控制器爲 UINavigationController:
// // AppDelegate.m // ContactsWithInput // // Created by Mike on 14-12-13. // Copyright (c) 2014年 Mike. All rights reserved. // #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; RootViewController *rootController = [[RootViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController]; //設置rootviewcontroller爲第一個控制器 //set navigation style navController.navigationBar.backgroundColor = [UIColor grayColor]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; } ...... @end
4. 修改RootViewController.m,實現tableview數據源protocol和用於編輯的delegate:
實現 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 用於告訴tableview總數據有多少航
實現 - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath用戶返回每行cell數據
// // RootViewController.m // ContactsWithInput // // Created by Mike on 14-12-13. // Copyright (c) 2014年 Mike. All rights reserved. // #import "RootViewController.h" #import "PersonInputViewController.h" #import "Person.h" #import "PersonService.h" @interface RootViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> { PersonService *_personService; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; UINavigationItem *navItem = self.navigationItem; //set navigation title navItem.title = @"全部聯繫人"; _personService = [[PersonService alloc] init]; //listen add btn UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)]; navItem.rightBarButtonItem = addBtn; } #pragma mark - 添加聯繫人 - (void)addPerson{ //goto person input view PersonInputViewController *piController = [[PersonInputViewController alloc] init]; piController.personService = _personService; piController.tableView = _tableView; [self.navigationController pushViewController:piController animated:YES]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _personService.persons.count; } - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"ID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]; } Person *p = _personService.persons[indexPath.row]; cell.textLabel.text = p.name; cell.detailTextLabel.text = p.phone; return cell; } #pragma mark - 點擊行進入編輯模式(須要優化爲先進入詳情頁面,再提供按鈕編輯) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Person *p = [_personService.persons objectAtIndex:indexPath.row]; //goto person input view PersonInputViewController *piController = [[PersonInputViewController alloc] init]; piController.personService = _personService; piController.tableView = _tableView; piController.person = p; [self.navigationController pushViewController:piController animated:YES]; } #pragma make - 提交tableview編輯操做 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle != UITableViewCellEditingStyleDelete) { return; } int row = indexPath.row; Person *p = _personService.persons[row]; NSString *msg = [NSString stringWithFormat:@"%@%@%@",@"肯定要刪除 \"", p.name, @"\" 嗎?"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"刪除聯繫人" message:msg delegate:self cancelButtonTitle:@"刪除" otherButtonTitles:@"取消", nil]; alert.tag = row; // 記錄所須要刪除的行號 [alert show]; } #pragma mark - 根據刪除提示執行操做 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //tableview退出編輯模式 _tableView.editing = NO; if(buttonIndex != 0) { return ; } int row = alertView.tag; //刪除源數據 [_personService.persons removeObjectAtIndex:row]; NSIndexPath *delIdx = [NSIndexPath indexPathForItem:row inSection:0]; [_tableView deleteRowsAtIndexPaths:@[delIdx] withRowAnimation:UITableViewRowAnimationTop]; } @end
數據來源由一個PersonService管理,用於加載數據,定義以下:
// // PersonService.h // ContactsWithEdit // // Created by Mike on 14-12-11. // Copyright (c) 2014年 Mike. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface PersonService : NSObject @property(atomic, strong) NSMutableArray *persons; @end
// // PersonService.m // ContactsWithEdit // // Created by Mike on 14-12-11. // Copyright (c) 2014年 Mike. All rights reserved. // #import "PersonService.h" #import "Person.h" @interface PersonService () { } @end @implementation PersonService -(id)init { self = [super init]; if(self) { _persons = [NSMutableArray array]; NSBundle *bundle = [NSBundle mainBundle]; NSArray *perResource = [NSMutableArray arrayWithContentsOfFile:[bundle pathForResource:@"person" ofType:@"plist"]]; for(NSDictionary *pDic in perResource) { Person *p = [Person personWithName:pDic[@"name"] phone:pDic[@"phone"]]; [_persons addObject:p]; } } return self; } @end
2.2 用戶添加和編輯界面設計
1. 界面很簡單,兩個label和兩個text input,以下圖:
把這兩個text input連線到 PersonInputViewController.h,因爲添加或者編輯後須要刷新數據源和表格,因此須要personService、tableview和person:
// // PersonInputViewController.h // ContactsWithInput // // Created by Mike on 14-12-13. // Copyright (c) 2014年 Mike. All rights reserved. // #import <UIKit/UIKit.h> #import "PersonService.h" #import "Person.h" @interface PersonInputViewController : UIViewController @property(nonatomic, strong)PersonService *personService; @property (weak, nonatomic)UITableView *tableView; //refresh tableview after insert @property(strong, nonatomic)Person *person; //obj for edit @property (weak, nonatomic) IBOutlet UITextField *nameField; // 名字輸入框 @property (weak, nonatomic) IBOutlet UITextField *phoneField; // 號碼輸入框 @end
PersonInputViewController 初始化時建立兩個按鈕《取消》和《完成》,並設置監聽方法,分別設置在左邊和右邊。當person不爲空的時候,表明進入編輯模式:
// // PersonInputViewController.m // ContactsWithInput // // Created by Mike on 14-12-13. // Copyright (c) 2014年 Mike. All rights reserved. // #import "PersonInputViewController.h" @interface PersonInputViewController () @end @implementation PersonInputViewController - (void)viewDidLoad { [super viewDidLoad]; //set bar button UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(backRootView)]; UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(savePerson)]; self.navigationItem.leftBarButtonItem = cancelBtn; self.navigationItem.rightBarButtonItem = saveBtn; //setter data for edit if(_person) { _nameField.text = _person.name; _phoneField.text = _person.phone; } } - (void)backRootView { [self.navigationController popViewControllerAnimated:YES]; //返回上一個view } - (void)savePerson { NSString *name = _nameField.text; NSString *phone = _phoneField.text; //remove white space name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; phone = [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if(![name isEqualToString:@""] || ![phone isEqualToString:@""]) { if(_person) { //edit _person.name = name; _person.phone = phone; //refresh tableview NSIndexPath *editIdx = [NSIndexPath indexPathForRow:[_personService.persons indexOfObject:_person] inSection:0]; [_tableView reloadRowsAtIndexPaths:@[editIdx] withRowAnimation:UITableViewRowAnimationMiddle]; }else { Person *p = [Person personWithName:name phone:phone]; [_personService.persons addObject:p]; //refresh tableview NSIndexPath *addIdx = [NSIndexPath indexPathForRow:_personService.persons.count-1 inSection:0]; [_tableView insertRowsAtIndexPaths:@[addIdx] withRowAnimation:UITableViewRowAnimationRight]; } } [self backRootView]; } @end
最後是一個實體類Person的定義:
// // Person.h // ContactsWithEdit // // Created by Mike on 14-12-10. // Copyright (c) 2014年 Mike. All rights reserved. // #import <Foundation/Foundation.h> @interface Person : NSObject +(Person*) personWithName:(NSString *)name phone:(NSString *) phone; @property(nonatomic, strong) NSString* name; @property(nonatomic, strong) NSString* phone; @end
// // Person.m // ContactsWithEdit // // Created by Mike on 14-12-10. // Copyright (c) 2014年 Mike. All rights reserved. // #import "Person.h" @implementation Person +(Person*) personWithName:(NSString *)name phone:(NSString *) phone { Person *p = [[Person alloc] init]; p.name = name; p.phone = phone; return p; } @end
工程下載地址:http://files.cnblogs.com/mikevictor07/ContactsWithInput.zip