本篇文章是對于復(fù)雜對象來說的 這里我們來實(shí)現(xiàn)一個通訊錄的功能 這個通訊錄一共有三個頁面:
(1) 主頁面 顯示名字的tableView
(2) 點(diǎn)擊主頁面中的cell可以跳轉(zhuǎn)到個人詳情頁面(在這個頁面中 可以在textField中修改聯(lián)系人的信息) 修改完信息后 點(diǎn)擊修改完成按鈕 跳轉(zhuǎn)回主頁 這時要將修改完的數(shù)據(jù)也返回到主頁 并在主頁將返回的數(shù)據(jù)添加到數(shù)組中
更新tableView (注意: 這時將數(shù)據(jù)返回主頁就不是簡單的add添加到數(shù)組中就行了 這里要注意插入所修改的數(shù)據(jù)的位置 因?yàn)槲覀兯薷牟灰欢ㄊ悄囊恍衏ell 所以當(dāng)我們在主頁點(diǎn)擊cell跳轉(zhuǎn)到第二頁修改頁的時候 要利用屬性傳值向第二頁傳一個位置的參數(shù) 也就是indexPath.row(做法是 在第二頁的.h文件中設(shè)置一個NSInteger屬性 在tableView的點(diǎn)擊方法中 將indexPath.row賦值給第二頁的NSInteger屬性) 當(dāng)修改完信息后 點(diǎn)擊修改完成 這時要返回主頁 我們要把修改完的數(shù)據(jù)也返回到主頁 我們用到協(xié)議傳值(創(chuàng)建協(xié)議 寫協(xié)議方法 設(shè)置協(xié)議屬性 簽訂協(xié)議 設(shè)置代理人 實(shí)現(xiàn)協(xié)議方法 觸發(fā)協(xié)議 協(xié)議方法應(yīng)該要返回兩個參數(shù)給主頁 (1)對象 也就是將修改完的對象返回到主頁 (2) 位置信息 也就是從主頁傳到第二頁的indexPath 我們還需要將他返回到主頁) 我們通過利用協(xié)議傳值 將修改好的對象和一個位置信息都傳到了主頁 我們在主頁中實(shí)現(xiàn)協(xié)議方法 也就是將返回來的對象添加到主頁數(shù)據(jù)數(shù)組中 由于這是在修改信息 所以要先把修改之前的信息刪除然后再添加這個返回來的修改完的對象 這是我們就要用到我們從主頁傳到第二頁的位置屬性indexPath 我們先刪除 再插入 這是數(shù)組中的信息就已經(jīng)更新完畢了 我們將這個更新完的數(shù)組進(jìn)行歸檔 覆蓋掉之前的舊數(shù)組就可以了)
(3) 在主頁面的navigationBar上創(chuàng)建一個右按鈕 點(diǎn)擊這個按鈕 跳轉(zhuǎn)到第三頁 第三頁的textField都是空的 我們在這里添加新的聯(lián)系人信息 然后點(diǎn)擊第三頁的"添加完成"按鈕 將添加好的信息(也就是對象)返回到主頁 將新添加的信息添加到主頁數(shù)據(jù)數(shù)組中 再將這個數(shù)組歸檔覆蓋掉原來的數(shù)據(jù) 這樣就添加完成了
除了添加聯(lián)系人信息 修改信息 我們還實(shí)現(xiàn)了刪除聯(lián)系人的功能 其實(shí)重要的是要記住無論我們做過了什么操作 只要處理好數(shù)據(jù)源將新的數(shù)據(jù)歸檔 覆蓋掉舊的數(shù)據(jù)就可以實(shí)現(xiàn)相應(yīng)的功能了
下面我們根據(jù)代碼來分析一下
-
首先我們還是先傳建一個window 將ViewController設(shè)置為根視圖
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.backgroundColor = [UIColor whiteColor]; ViewController *vc = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = nav; return YES; }
-
我們在根視圖控制器中創(chuàng)建tableView
#import "ViewController.h"
#define kTableView @"reuse"
#import "Contact.h"
#import "ContentViewController.h"
#import "AddViewController.h"@interface ViewController () <UITableViewDelegate, UITableViewDataSource, ContentViewControllerDelegate, AddViewControllerDelegate> @property (nonatomic, strong) UITableView *contactTableView; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, copy) NSString *arrayPath; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationController.navigationBar.backgroundColor = [UIColor grayColor]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemI temAdd target:self action:@selector(click:)]; [self createTableView]; [self createData]; } - (void)click:(UIButton *)button { // 跳轉(zhuǎn)到添加頁面 AddViewController *vc = [[AddViewController alloc] init]; // 在跳轉(zhuǎn)到田間頁面的點(diǎn)擊方法中指定代理人 vc.delegate = self; [self.navigationController pushViewController:vc animated:YES]; } - (void)sendAddContact:(Contact *)contact { // 在主頁中實(shí)現(xiàn)添加頁面的協(xié)議方法 // 將從添加頁傳來的新數(shù)據(jù)添加到數(shù)據(jù)數(shù)組中 [self.dataArray addObject:contact]; // 刷新tableView [self.contactTableView reloadData]; // 將添加數(shù)據(jù)后的新數(shù)組歸檔本地 覆蓋之前的數(shù)據(jù) [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath]; } - (void)createData { // 創(chuàng)建沙盒路徑 NSArray *sandArray = NSSearchPathForDirectoriesInDomains(NSDocum entDirectory, NSUserDomainMask, YES); NSString *path = [sandArray firstObject]; // 拼接一個路徑 這個路徑就是我們數(shù)據(jù)的路徑 self.arrayPath = [path stringByAppendingPathComponent:@"contactPerson.plist"]; // 我們通過之前的路徑 反歸檔 從本地返回一個數(shù)組 NSArray *unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:_arrayPath]; // 判斷 如果數(shù)組中內(nèi)容不為空 就把這個反歸檔的數(shù)組賦值給我們的數(shù)據(jù)數(shù)組 if (unarchiveArray.count != 0) { self.dataArray = [NSMutableArray arrayWithArray:unarchiveArray]; } else { // 如果反歸檔返回的是一個空的數(shù)組我們就向我們的數(shù)據(jù)數(shù)組中添加一個對象(其實(shí)都不需要這個判斷的 我們就直接將反歸檔 返回的數(shù)組直接賦值給我們的數(shù)據(jù)數(shù)組就可以 這里 我們?yōu)榱瞬蛔屩黜撌强瞻椎?就判斷了一下 如果是空的就賦一個初始的對象) Contact *contact1 = [[Contact alloc] initWithPicImage:@"fsds" name:@"Eason" phoneNum:@"123456" address:@"香港"]; self.dataArray = [NSMutableArray array]; [_dataArray addObject:contact1]; [NSKeyedArchiver archiveRootObject:_dataArray toFile:_arrayPath]; } NSLog(@"%@", _arrayPath); } - (void)createTableView { self.contactTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.contactTableView.delegate = self; self.contactTableView.dataSource = self; [self.view addSubview:self.contactTableView]; [self.contactTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView]; } // 實(shí)現(xiàn)刪除功能 這是tableView編輯中的內(nèi)容 - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 要想刪除聯(lián)系人 我們要清楚都要刪除哪些內(nèi)容 首先要實(shí)現(xiàn)刪除我們的數(shù)據(jù)數(shù)組相對應(yīng)的對象數(shù)據(jù)然后要刪除對應(yīng)的tableView的cell() 然后就是處理之前本地存儲的數(shù)據(jù)了 我們只需要將更新后的數(shù)據(jù)數(shù)組再次歸檔就可以更新本地數(shù)據(jù)了 [self.dataArray removeObjectAtIndex:indexPath.row]; [self.contactTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath]; [self.contactTableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView forIndexPath:indexPath]; cell.textLabel.text = [self.dataArray[indexPath.row] name]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ContentViewController *vc = [[ContentViewController alloc] init]; // 簽訂修改信息的協(xié)議 vc.delegate = self; // 將數(shù)據(jù)所在數(shù)組的下標(biāo)傳值給修改頁面 vc.index = indexPath.row; vc.contact = self.dataArray[indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } // 實(shí)現(xiàn)修改頁的協(xié)議方法 - (void)sendContact:(Contact *)contact index:(NSInteger)index { // 先移除數(shù)據(jù)數(shù)組中對應(yīng)位置的數(shù)據(jù) [self.dataArray removeObjectAtIndex:index]; // 再將修改后的數(shù)據(jù)插入到數(shù)組中 [self.dataArray insertObject:contact atIndex:index]; [self.contactTableView reloadData]; // 再將更新后的數(shù)組歸檔 覆蓋掉之前的舊數(shù)據(jù) [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath]; }
-
添加新聯(lián)系人頁面
#import <UIKit/UIKit.h> #import "Contact.h" // 創(chuàng)建協(xié)議 利用協(xié)議實(shí)現(xiàn)將添加頁添加的數(shù)據(jù)傳到主頁面 @protocol AddViewControllerDelegate <NSObject> - (void)sendAddContact:(Contact *)contact; @end @interface AddViewController : UIViewController @property (nonatomic, strong) Contact *contact; @property (nonatomic, weak) id<AddViewControllerDelegate>delegate; @end #import "AddViewController.h" @interface AddViewController () @property (nonatomic, strong) UIImageView *picImageView; @property (nonatomic, strong) UITextField *nameTextField; @property (nonatomic, strong) UITextField *phoneNumTextField; @property (nonatomic, strong) UITextField *addressTextField; @property (nonatomic, strong) UIButton *addButton; @end @implementation AddViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent = NO; [self createView]; } - (void)createView { UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 240, 60, 30)]; nameLabel.text = @"姓名"; [self.view addSubview:nameLabel]; UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 340, 60, 30)]; phoneLabel.text = @"電話"; [self.view addSubview:phoneLabel]; UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 290, 60, 30)]; addressLabel.text = @"地址"; [self.view addSubview:addressLabel]; self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 10, 150, 200)]; self.picImageView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.picImageView]; self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 240, 200, 30)]; [self.view addSubview:self.nameTextField]; self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 290, 200, 30)]; [self.view addSubview:self.addressTextField]; self.phoneNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 340, 200, 30)]; [self.view addSubview:self.phoneNumTextField]; self.addButton = [UIButton buttonWithType:UIButtonTypeSystem]; self.addButton.frame = CGRectMake(80, 390, 90, 30); [self.addButton setTitle:@"添加完成" forState:UIControlStateNormal]; [self.view addSubview:self.addButton]; [self.addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside]; } - (void)addClick:(UIButton *)button { // 在添加完成的點(diǎn)擊方法中創(chuàng)建一個對象 給對象進(jìn)行賦值 self.contact = [[Contact alloc] initWithPicImage:@"u=3808451166,1723806820&fm=23&gp=0" name:self.nameTextField.text phoneNum:self.phoneNumTextField.text address:self.addressTextField.text]; // 這里觸發(fā)協(xié)議方法 將添加的信息傳到主頁 [self.delegate sendAddContact:self.contact]; [self.navigationController popToRootViewControllerAnimated:YES]; }
-
修改聯(lián)系人信息的頁面
#import <UIKit/UIKit.h> #import "Contact.h" // 創(chuàng)建協(xié)議 利用協(xié)議將修改后的數(shù)據(jù)傳到主頁 @protocol ContentViewControllerDelegate <NSObject> // 修改 - (void)sendContact:(Contact *)contact index:(NSInteger)index; @end @interface ContentViewController : UIViewController @property (nonatomic, assign) NSInteger index; @property (nonatomic, strong) Contact *contact; @property (nonatomic, weak) id <ContentViewControllerDelegate>delegate; @end #import "ContentViewController.h" @interface ContentViewController () @property (nonatomic, strong) UIImageView *picImageView; @property (nonatomic, strong) UITextField *nameTextField; @property (nonatomic, strong) UITextField *phoneNumTextField; @property (nonatomic, strong) UITextField *addressTextField; @property (nonatomic, strong) UIButton *changeButton; @property (nonatomic, strong) NSMutableDictionary *textFieldDic; @end @implementation ContentViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent = NO; [self createContentView]; } - (void)createContentView { // 創(chuàng)建label UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 240, 60, 30)]; nameLabel.text = @"姓名"; [self.view addSubview:nameLabel]; UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 340, 60, 30)]; phoneLabel.text = @"電話"; [self.view addSubview:phoneLabel]; UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 290, 60, 30)]; addressLabel.text = @"地址"; [self.view addSubview:addressLabel]; self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 10, 150, 200)]; self.picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.contact.picImage]]; self.picImageView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.picImageView]; self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 240, 200, 30)]; self.nameTextField.text = self.contact.name; [self.view addSubview:self.nameTextField]; self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 290, 200, 30)]; self.addressTextField.text = self.contact.address; [self.view addSubview:self.addressTextField]; self.phoneNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 340, 200, 30)]; self.phoneNumTextField.text = self.contact.phoneNum; [self.view addSubview:self.phoneNumTextField]; self.changeButton = [UIButton buttonWithType:UIButtonTypeSystem]; self.changeButton.frame = CGRectMake(80, 390, 90, 30); [self.changeButton setTitle:@"修改完成" forState:UIControlStateNormal]; [self.view addSubview:self.changeButton]; [self.changeButton addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside]; } // 修改信息 - (void)changeClick:(UIButton *)button { self.contact = [[Contact alloc] initWithPicImage:@"u=3808451166,1723806820&fm=23&gp=0.jpg" name:self.nameTextField.text phoneNum:self.phoneNumTextField.text address:self.addressTextField.text]; [self.delegate sendContact:self.contact index:self.index]; [self.navigationController popToRootViewControllerAnimated:YES]; }