新浪微博 有道云笔记 麦库 EverNote Pocket Instapaper 更多

联系人数据存储Demo源代码

  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) 2013itcast. 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) 2013itcast. 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) 2013itcast. 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) 2013itcast. 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) 2013itcast. 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) 2013itcast. 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
posted @ 2013-12-25 14:26  iTeaTime(技术清谈)  阅读(422)  评论(0编辑  收藏  举报