Objective-C
//
// ContactsViewController.m
// 01- 私人通讯录
//
// Created by HiJack on 15/12/10.
// Copyright © 2015年 HiJack. All rights reserved.
//
#import "ContactsViewController.h"
#import "AddViewController.h"
#import "Contact.h"
#import "EditViewController.h"
#import "DeleteViewController.h"
@interface ContactsViewController () <AddViewControllerDelegate, EditViewControllerDelegate>
- (IBAction)logout:(id)sender;
- (IBAction)clickTrushItem:(UIBarButtonItem *)sender;
@end
@implementation ContactsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Tableview数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contacts.count ;
}
#define ID @"contact"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// if (cell == nil) {
// cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
// }
// 2.设置cell数据
Contact *contact = self.contacts[indexPath.row];
cell.textLabel.text = contact.name;
cell.detailTextLabel.text = contact.phoneNum;
return cell;
}
// 数据操作 删除或插入
// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
// 向左滑动row调用该方法,可实现滑动删除或增加
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 1.删除模型数据(model)
[self.contacts removeObjectAtIndex:indexPath.row];
// 2.删除指定行 (view)
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 3.归档
[self archiveContacts:self.contacts];
// 4刷新列表
[self.tableView reloadData];
}
}
#pragma mark - Tableview代理方法
// 可以不实现,默认为删除. (另外一个是UITableViewCellEditingStyleInsert
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// return UITableViewCellEditingStyleDelete;
//}
#pragma mark - 点击事件
- (IBAction)logout:(id)sender {
// 创建UIAlertControlller对象(设置标题,内容,显示风格为ActionSheet)
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"确定要注销吗" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// 创建UIAlertAction对象(按钮标题、风格为Default,Cancel以及Destructive 、action执行后续动作)
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler: ^(UIAlertAction * action) {
// 返回上一界面
[self.navigationController popViewControllerAnimated:YES];
}];
// UIAlerController加入Action
[sheet addAction:cancelAction];
[sheet addAction:okAction];
// 显示UIAlertControlller对象
[self presentViewController:sheet animated:YES completion:nil];
}
/**
* 切换编辑状态(内部调用 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath)
*
* @param sender 垃圾箱按钮
*/
- (IBAction)clickTrushItem:(UIBarButtonItem *)sender {
[self.tableView setEditing:! self.tableView.editing animated:YES];
}
/**
* segua跳转前调用的方法
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id vc = segue.destinationViewController;
if ([vc isKindOfClass:[AddViewController class]]) { // 跳转到添加联系人的控制器
AddViewController *addViewController = segue.destinationViewController;
addViewController.delegate = self;
} else if ([vc isKindOfClass:[EditViewController class]]) { // 跳转到查看联系人的控制器
EditViewController *editVc = vc;
// 方法1:
// UITableViewCell *cell = sender;
// editVc.contact = [self contactInCell:cell];
// NSLog(@"%@",cell.textLabel.text );
// 方法2:(最好)
// 取得选中的行号
editVc.delegate = self;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
editVc.indexPath = indexPath;
editVc.contact = self.contacts[indexPath.row];
// 设置控制器标题
} else if ([vc isKindOfClass:[DeleteViewController class]]) {
DeleteViewController *deleteVc = vc;
deleteVc.contacts = self.contacts;
}
}
#pragma mark - 添加contact并归档
- (void)addViewControllerAddToContact:(AddViewController *)addViewController addContact:(Contact *)contact
{
// 添加数据模型
[self.contacts addObject:contact];
// 归档
[self archiveContacts:self.contacts];
// 刷新列表
[self.tableView reloadData];
}
#pragma mark - 代理方法实现:EditViewController
- (void)editAndSave:(Contact *)contact withIndexPath:(NSIndexPath *)indexPath
{
// [self.contacts replaceObjectAtIndex:indexPath.row withObject:contact];
[self.tableView reloadData];
}
#pragma mark - 自定义归档
- (void)archiveContacts:(NSMutableArray *)contacts
{
// 如果模型有数据才进行归档
if (contacts.count) {
NSMutableArray *dictArray = [NSMutableArray array];
// 遍历数据模型
for (Contact *contact in contacts) {
NSDictionary *dict = [[NSDictionary alloc]init];
// Returns a dictionary containing the property values identified by each of the keys in a given array.
dict = [contact dictionaryWithValuesForKeys:@[@"name",@"phoneNum"]];
[dictArray addObject:dict];
}
// 1. document路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)lastObject ];
// 2. 拼接文件路径
NSString *contactPlist = [docPath stringByAppendingPathComponent:@"contacts.plist"];
[dictArray writeToFile:contactPlist atomically:YES];
}
}
#pragma mark - 自定义读档
- (NSMutableArray *)readContactsFromFile
{
// 1. document路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)lastObject ];
NSLog(@"%@",docPath );
// 2. 拼接文件路径
NSString *contactPlist = [docPath stringByAppendingPathComponent:@"contacts.plist"];
NSMutableArray *contacts = [NSMutableArray array];
NSMutableArray *dictArray = [NSMutableArray arrayWithContentsOfFile:contactPlist];
for (NSDictionary *dict in dictArray) {
Contact *contact = [[Contact alloc]init];
[contact setValuesForKeysWithDictionary:dict];
[contacts addObject:contact];
}
return contacts;
}
//#pragma mark - 自定义self方法
//- (Contact *)contactInCell:(UITableViewCell *)cell
//{
// Contact *contact = [[Contact alloc]init];
// contact.name = cell.textLabel.text;
// contact.phoneNum = cell.detailTextLabel.text;
// return contact;
//}
@end
good song