1. 源码下载地址:07-联系人数据存储.zip
    35.8 KB
  2. // MJPerson.h

  3. //
  4. //  MJPerson.h
  5. //  07-联系人数据存储
  6. //
  7. //  Created by apple on 13-12-11.
  8. //  Copyright (c) 2013年itcast. All rights reserved.
  9. ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html
  10. #import<Foundation/Foundation.h>
  11. @interface MJPerson : NSObject <NSCoding>
  12. @property (nonatomic,copy) NSString *name;
  13. @property (nonatomic,copy) NSString *phone;
  14. @end
  15. // MJPerson.m

    Map

  16. //
  17. //  MJPerson.m
  18. //  07-联系人数据存储
  19. //
  20. //  Created by apple on 13-12-11.
  21. //  Copyright (c) 2013年itcast. All rights reserved.
  22. //
  23. #import"MJPerson.h"
  24. @implementation MJPerson
  25. - (void)encodeWithCoder:(NSCoder *)encoder
  26. {
  27.     [encoder encodeObject:_name forKey:@"name"];
  28.     [encoder encodeObject:_phone forKey:@"phone"];
  29. }
  30. - (id)initWithCoder:(NSCoder *)decoder
  31. {
  32.    if(self= [super init]) {
  33.         _name = [decoder decodeObjectForKey:@"name"];
  34.         _phone = [decoder decodeObjectForKey:@"phone"];
  35.     }
  36.    return self;
  37. }
  38. @end
  39. // MJFriendsViewController.h

    Map

  40. //
  41. //  MJFriendsViewController.h
  42. //  07-联系人数据存储
  43. //
  44. //  Created by apple on 13-12-11.
  45. //  Copyright (c) 2013年itcast. All rights reserved.
  46. //
  47. #import<UIKit/UIKit.h>
  48. @interface MJFriendsViewController : UITableViewController
  49. @end
  50. // MJFriendsViewController.m

    Map

  51. //
  52. //  MJFriendsViewController.m
  53. //  07-联系人数据存储
  54. //
  55. //  Created by apple on 13-12-11.
  56. //  Copyright (c) 2013年itcast. All rights reserved.
  57. ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html
  58. #define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"persons.data"]
  59. #import "MJFriendsViewController.h"
  60. #import "MJAddViewController.h"
  61. #import "MJPerson.h"
  62. @interface MJFriendsViewController () <MJAddViewControllerDelegate>
  63. {
  64.     NSMutableArray *_persons;
  65. }
  66. @end
  67. @implementation MJFriendsViewController
  68. - (void)viewDidLoad
  69. {
  70.     [super viewDidLoad];
  71.    
  72. //    NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  73.    
  74. //    NSString *path = [doc stringByAppendingPathComponent:@"persons.data"];
  75.    
  76. //    _persons = [PersonTool persons];
  77.    
  78.     _persons = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
  79.    
  80.    if(_persons ==nil) {
  81.         _persons = [NSMutableArray array];
  82.     }
  83. }
  84. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  85. {
  86.    // 1.导航控制器
  87.     UINavigationController *nav = segue.destinationViewController;
  88.    
  89.    // 2.取出栈顶的添加控制器
  90.     MJAddViewController *add = (MJAddViewController *)nav.topViewController;
  91.    
  92.    // 3.设置代理
  93.     add.delegate =self;
  94. }
  95. #pragma mark - MJAddViewController的代理方法
  96. #pragma mark成功添加一个联系人就会调用
  97. - (void)addViewController:(MJAddViewController *)add didAddPerson:(MJPerson *)person
  98. {
  99.    // 1.将人塞进数组中
  100.     [_persons insertObject:person atIndex:0];
  101.    
  102.    // 2.刷新表格
  103.     [self.tableView reloadData];
  104.    
  105.    // 3.归档
  106. //    NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  107. ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html   
  108. //    NSString *path = [doc stringByAppendingPathComponent:@"persons.data"];
  109.    
  110.     [NSKeyedArchiver archiveRootObject:_persons toFile:kFilePath];
  111.    
  112. //    [PersonTool savePersons:_persons];
  113. }
  114. #pragma mark - Table view data source
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117.    return _persons.count;
  118. }
  119. #pragma mark每一行显示怎样的cell(内容)
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122.    // 1.定义一个标识
  123.    static NSString *ID =@"cell";
  124.    
  125.    // 2.去缓存池中取出可循环利用的cell
  126.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  127.    
  128.    // 3.如果缓存中没有可循环利用的cell
  129.    if(cell ==nil) {
  130.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  131.     }
  132.    
  133.    // 4.取出模型设置数据
  134.     MJPerson *p = _persons[indexPath.row];
  135.    
  136.     cell.textLabel.text = p.name;
  137.     cell.detailTextLabel.text = p.phone;
  138.    
  139.    return cell;
  140. }
  141. @end
  142. // MJAddViewController.h

    Map

  143. //
  144. //  MJAddViewController.h
  145. //  07-联系人数据存储
  146. //
  147. //  Created by apple on 13-12-11.
  148. //  Copyright (c) 2013年itcast. All rights reserved.
  149. //
  150. #import<UIKit/UIKit.h>
  151. @classMJAddViewController, MJPerson;
  152. @protocolMJAddViewControllerDelegate <NSObject>
  153. @optional
  154. - (void)addViewController:(MJAddViewController *)add didAddPerson:(MJPerson *)person;
  155. @end
  156. @interface MJAddViewController : UIViewController
  157. - (IBAction)cancel:(id)sender;
  158. - (IBAction)add:(id)sender;
  159. @property(weak,nonatomic)IBOutletUITextField *nameField;
  160. @property(weak,nonatomic)IBOutletUITextField *phoneField;
  161. @property(nonatomic,weak)id<MJAddViewControllerDelegate> delegate;
  162. @end
  163. // MJAddViewController.m

    Map

  164. //
  165. //  MJAddViewController.m
  166. //  07-联系人数据存储
  167. //
  168. //  Created by apple on 13-12-11.
  169. //  Copyright (c) 2013年itcast. All rights reserved.
  170. //
  171. #import"MJAddViewController.h"
  172. #import"MJPerson.h"
  173. @interfaceMJAddViewController ()
  174. @end
  175. @implementationMJAddViewController
  176. - (void)viewDidLoad
  177. {
  178.     [superviewDidLoad];
  179. // Do any additional setup after loading the view.
  180. }
  181. - (IBAction)cancel:(id)sender {
  182.     [self dismissViewController Animated:YES completion:nil];
  183. }
  184. - (IBAction)add:(id)sender {
  185.    if([_delegate respondsToSelector:@selector(addViewController:didAddPerson:)]) {
  186.         MJPerson *p = [[MJPerson alloc] init];
  187.         p.name = _nameField.text;
  188.         p.phone = _phoneField.text;
  189.        
  190.         [_delegate addViewController:self didAddPerson:p];
  191.        
  192.         [self dismissViewControllerAnimated:YEScompletion:nil];
  193.     }
  194. }
  195. @end

最新文章

  1. mysql配置远程连接方法之一(改表法)
  2. Makefile编译
  3. s2 devMode cmdshell
  4. Codeigniter
  5. js 获得日期相差天数
  6. [LeetCode]题解(python):110 Balanced Binary Tree
  7. 多线程编程之Linux环境下的多线程(二)
  8. Qt之进程间通信(Windows消息)
  9. 第四章:更多的bash shell命令
  10. QM课程02-外部功能
  11. ios--uitextfield动态限制输入的字数(解决方式)
  12. 提升PHP性能的21种方法
  13. 【Linux】鸟哥的Linux私房菜基础学习篇整理(二)
  14. Windows环境下 配置memcached (php)
  15. android更新SDK时候丢失annotations.jar 导致支持库报错
  16. UVA 12538 Version Controlled IDE 解题报告
  17. hashchange
  18. Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】
  19. Spark性能调优之解决数据倾斜
  20. 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?

热门文章

  1. SpringBoot学习:IDEA中快速搭建springboot项目
  2. golang select 退出结束goroutine
  3. NSDictionary底层实现原理
  4. ASP.NET Web API 2 返回 Json格式
  5. jqgrid-parmNames和jsonReader的使用,以及json的返回格式(转)
  6. vs code 在终端下使用 code ./ 打开当前项目
  7. CentOS环境配置Hadoop(一)
  8. sqlserver 找出字符第N次出现的位置
  9. C++ 递归读取目录下所有文件
  10. Sping工作原理