添加联系人:
#import "AddContactViewController.h" #import "AppDelegate.h" #import "Contact+Create.h" @interface AddContactViewController () @property (nonatomic, strong) NSManagedObjectContext *context; @end @implementation AddContactViewController @synthesize context = _context; - (void)loadView { self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 44.0f)]; nameTextField.placeholder = @"姓名"; nameTextField.tag = 100; [self.view addSubview:nameTextField]; UITextField *abbreviationTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 80.0f, 280.0f, 44.0f)]; abbreviationTextField.placeholder = @"姓名缩写 请输入字母"; abbreviationTextField.tag = 101; [self.view addSubview:abbreviationTextField]; } - (void)viewDidLoad { [super viewDidLoad]; self.context = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext; self.navigationItem.title = @"添加联系人"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)]; } - (void)save { UITextField *nameTextField = (UITextField *)[self.view viewWithTag:100]; UITextField *abbreviationTextField = (UITextField *)[self.view viewWithTag:101]; if ([nameTextField.text length] > 0 && [abbreviationTextField.text length] == 1) { // text type checking ?? !!! YES! NSInteger unique = arc4random()%100000; NSString *uniqueTemp = [NSString stringWithFormat:@"%d",unique]; // unique? need to enter unique numner is unique . Haha..! NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:nameTextField.text, @"name", abbreviationTextField.text, @"abbreviation", uniqueTemp , @"unique", nil]; [Contact saveContactWithDictionary:dic InManagedObjectContext:self.context]; [self.navigationController popViewControllerAnimated:YES]; } else { NSLog(@"sleep..oop..."); } } @end
MainList方式:
#import "ContactListViewController.h" #import "AppDelegate.h" #import "Tool.h" #import "UIImageView+WebCache.h" #import "ContactViewController.h" #import "AddContactViewController.h" @interface ContactListViewController ()<UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSManagedObjectContext *context; @property (nonatomic, strong) NSFetchedResultsController*fetchedResultsController; @property (nonatomic, strong) NSArray *sectionTitle; @end @implementation ContactListViewController @synthesize tableView = _tableView; @synthesize context = _context; @synthesize fetchedResultsController = _fetchedResultsController; @synthesize sectionTitle = _sectionTitle; #pragma mark - #pragma mark - Private - (void)addContact // write a simple example , only need to fill in NAME { AddContactViewController *addContactViewController = [[AddContactViewController alloc] init]; [self.navigationController pushViewController:addContactViewController animated:YES]; // maybe use present*** ,this is the most logical answer in my view. } - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Contact"]; request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"abbreviation" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]]; _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:@"abbreviation" cacheName:nil]; _fetchedResultsController.delegate = self; [_fetchedResultsController performFetch:NULL]; return _fetchedResultsController; } #pragma mark - #pragma mark - Life - (void)loadView { self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } - (void)viewDidLoad { [super viewDidLoad]; self.context = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext; self.navigationItem.title = @"联系人"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact)]; //create text data [Tool createTextDataWithManagedObjectContext:self.context]; self.sectionTitle = [Tool createSectionNameTitles]; } #pragma mark - #pragma mark - UITableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[[self.fetchedResultsController sections] objectAtIndex:section] name]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { if ([self.fetchedResultsController.sectionIndexTitles containsObject:title]) { NSLog(@"index %d",index); NSInteger newIndex = [self.fetchedResultsController.sectionIndexTitles indexOfObject:title]; NSLog(@"newindex %d",newIndex); return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:newIndex]; } else { return 0; } } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.sectionTitle; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ContactListCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } Contact *contact = [self.fetchedResultsController objectAtIndexPath:indexPath]; cell.textLabel.text = contact.name; [cell.imageView setImageWithURL:[NSURL URLWithString:contact.avatar] placeholderImage:[UIImage imageNamed:@"touxiang"]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 45.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Contact *contact = [self.fetchedResultsController objectAtIndexPath:indexPath]; ContactViewController *contactViewController = [[ContactViewController alloc] initWithContact:contact]; [self.navigationController pushViewController:contactViewController animated:YES]; } #pragma mark - NSFetchedResultControllerDelegate - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { switch (type) { case NSFetchedResultsChangeInsert: [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; default: break; } } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { switch (type) { case NSFetchedResultsChangeInsert: [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeMove: [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; default: break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } @end
contact的一个item显示:
#import "ContactViewController.h" #import "UIImageView+WebCache.h" @interface ContactViewController () @property (nonatomic, strong) Contact *contact; @end @implementation ContactViewController @synthesize contact = _contact; #pragma mark- Life - (id)initWithContact:(Contact *)contact { if (self = [super init]) { self.contact = contact; } return self; } - (void)loadView { self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.navigationItem.title = self.contact.name; UIImageView *avatarView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 45.0f, 45.0f)]; [avatarView setImageWithURL:[NSURL URLWithString:self.contact.avatar] placeholderImage:[UIImage imageNamed:@"touxiang"]]; [self.view addSubview:avatarView]; UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65.0f, 10.0f, 130.0f, 22.0f)]; nameLabel.text = self.contact.name; [self.view addSubview:nameLabel]; UILabel *mobileLabel = [[UILabel alloc] initWithFrame:CGRectMake(65.0f, 100.0f, 160.0f, 22.0f)]; mobileLabel.text = self.contact.mobile; [self.view addSubview:mobileLabel]; //............. add all information from self.contact.name . If we need to edit , we can use textField; } - (void)viewDidLoad { [super viewDidLoad]; } @end
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦