#import
"FirstViewController.h"
@interface FirstViewController ()
// 定义字典属性提供数据支持 ( 存储联系人 )
@property ( nonatomic , retain ) NSMutableDictionary *contectDic;
// 定义数组接受字典中的键;
@property ( nonatomic , retain ) NSMutableArray *keys;
// 创建属性用来拨号
@property ( nonatomic , retain ) UIWebView *phoneWebView;
// 创建 tableView 的属性
@property ( nonatomic , retain ) UITableView *tableView;
@end
@implementation FirstViewController
- ( void )dealloc {
self . tableView = nil ;
self . contectDic = nil ;
self . keys = nil ;
self . phoneWebView = nil ;
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
// 获取 plist 的文件路径
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"Contacts" ofType : @"plist" ];
// 读取 plist 文件的内容数据
self . contectDic = [ NSMutableDictionary dictionaryWithContentsOfFile :filePath];
// 对字典里的键进行排序,返回一个数组
self . keys = [[[[ self . contectDic allKeys ] sortedArrayUsingSelector : @selector (compare:)] mutableCopy ] autorelease ];
// 创建 tableView 对象
self . tableView = [[ UITableView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds style : UITableViewStylePlain ];
// 设置代理人对象提供数据源
self . tableView . dataSource = self ;
// 设置代理人提供 tableViewcell 的操作
self . tableView . delegate = self ;
// 设置 tableView 的分割线颜色
self . tableView . separatorColor = [ UIColor redColor ];
// 添加对象到控制器
[ self . view addSubview : self . tableView ];
// 释放所有权
[ self . tableView release ];
/*
// 设置编辑按钮
*/
// 视图控制器已经为我们提供了一个编辑按钮
self . navigationItem . rightBarButtonItem = self . editButtonItem ;
}
#pragma mark EditeAction
- ( void )setEditing:( BOOL )editing animated:( BOOL )animated {
// 如果不走这个方法, tableView 就会一直处于编辑状态
[ super setEditing :editing animated :animated];
// 让 tableView 处于编辑状态
[ self . tableView setEditing :editing animated :animated];
}
#pragma mark-Datesource
// 返回多少个分区
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView {
return self . keys . count ;
// return self.contectDic.count;
}
// 返回每一个分区多少行
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section {
// 返回每一个分区的联系人个数
return [ self . contectDic [ self . keys [section]] count ];
}
// 每一行显示的内容
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
static NSString *ID = @"cell" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :ID];
if (cell == nil ) {
cell = [[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleValue1 reuseIdentifier :ID] autorelease ];
}
// 设置每一个 cell 的内容
NSDictionary *contect = self . contectDic [ self . keys [indexPath. section ]][indexPath. row ];
// 展示数据
// 加载头像
cell. imageView . image = [ UIImage imageNamed :contect[ @"image" ]];
// 加载名字
cell. textLabel . text = contect[ @"name" ];
// 加载电话
cell. detailTextLabel . text = contect[ @"phone" ];
// 设置 cell 辅助视图
// 将图片进行等比压缩, 参数 1 代表要压缩的图片, 参数 2 代表压缩的比例 (0-1 放大 大于 1 代表缩小, 参数 3 :压缩的方向 )
UIImage *image = [ UIImage imageWithCGImage :[ UIImage imageNamed : @"phone" ]. CGImage scale : 25 orientation : UIImageOrientationUp ];
UIImageView *imageView = [[ UIImageView alloc ] initWithImage :image];
cell. accessoryView = imageView;
// 打开 imageView 的用户交互
imageView. userInteractionEnabled = YES ;
// 为 imageView 添加一个手势
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tapAction:)];
[imageView addGestureRecognizer :tap];
[tap release ];
return cell;
}
// 实现打电话功能
- ( void )tapAction:( UITapGestureRecognizer *)sender {
// 获取点击的 cell
UITableViewCell *cell = ( UITableViewCell *)sender. view . superview ;
// 获取电话
//tel 打电话 sms 发短信
NSString *tel = [ NSString stringWithFormat : @"tel://%@" , cell. detailTextLabel . text ];
// 进行拨号操作 ( 该方法无法通过苹果审核,非苹果官方操作不能上传到 AppStore 上 )
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
// 苹果可以通过的审核的拨号操作 可以上传到 AppStore 上
self . phoneWebView = [[ UIWebView alloc ] initWithFrame : CGRectZero ];
if ( self . phoneWebView == nil ) {
self . phoneWebView = [[[ UIWebView alloc ] initWithFrame : CGRectZero ] autorelease ];
}
// 开始拨号
[ self . phoneWebView loadRequest :[ NSURLRequest requestWithURL :[ NSURL URLWithString :tel]]];
@interface FirstViewController ()
// 定义字典属性提供数据支持 ( 存储联系人 )
@property ( nonatomic , retain ) NSMutableDictionary *contectDic;
// 定义数组接受字典中的键;
@property ( nonatomic , retain ) NSMutableArray *keys;
// 创建属性用来拨号
@property ( nonatomic , retain ) UIWebView *phoneWebView;
// 创建 tableView 的属性
@property ( nonatomic , retain ) UITableView *tableView;
@end
@implementation FirstViewController
- ( void )dealloc {
self . tableView = nil ;
self . contectDic = nil ;
self . keys = nil ;
self . phoneWebView = nil ;
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
// 获取 plist 的文件路径
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"Contacts" ofType : @"plist" ];
// 读取 plist 文件的内容数据
self . contectDic = [ NSMutableDictionary dictionaryWithContentsOfFile :filePath];
// 对字典里的键进行排序,返回一个数组
self . keys = [[[[ self . contectDic allKeys ] sortedArrayUsingSelector : @selector (compare:)] mutableCopy ] autorelease ];
// 创建 tableView 对象
self . tableView = [[ UITableView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds style : UITableViewStylePlain ];
// 设置代理人对象提供数据源
self . tableView . dataSource = self ;
// 设置代理人提供 tableViewcell 的操作
self . tableView . delegate = self ;
// 设置 tableView 的分割线颜色
self . tableView . separatorColor = [ UIColor redColor ];
// 添加对象到控制器
[ self . view addSubview : self . tableView ];
// 释放所有权
[ self . tableView release ];
/*
// 设置编辑按钮
*/
// 视图控制器已经为我们提供了一个编辑按钮
self . navigationItem . rightBarButtonItem = self . editButtonItem ;
}
#pragma mark EditeAction
- ( void )setEditing:( BOOL )editing animated:( BOOL )animated {
// 如果不走这个方法, tableView 就会一直处于编辑状态
[ super setEditing :editing animated :animated];
// 让 tableView 处于编辑状态
[ self . tableView setEditing :editing animated :animated];
}
#pragma mark-Datesource
// 返回多少个分区
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView {
return self . keys . count ;
// return self.contectDic.count;
}
// 返回每一个分区多少行
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section {
// 返回每一个分区的联系人个数
return [ self . contectDic [ self . keys [section]] count ];
}
// 每一行显示的内容
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
static NSString *ID = @"cell" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :ID];
if (cell == nil ) {
cell = [[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleValue1 reuseIdentifier :ID] autorelease ];
}
// 设置每一个 cell 的内容
NSDictionary *contect = self . contectDic [ self . keys [indexPath. section ]][indexPath. row ];
// 展示数据
// 加载头像
cell. imageView . image = [ UIImage imageNamed :contect[ @"image" ]];
// 加载名字
cell. textLabel . text = contect[ @"name" ];
// 加载电话
cell. detailTextLabel . text = contect[ @"phone" ];
// 设置 cell 辅助视图
// 将图片进行等比压缩, 参数 1 代表要压缩的图片, 参数 2 代表压缩的比例 (0-1 放大 大于 1 代表缩小, 参数 3 :压缩的方向 )
UIImage *image = [ UIImage imageWithCGImage :[ UIImage imageNamed : @"phone" ]. CGImage scale : 25 orientation : UIImageOrientationUp ];
UIImageView *imageView = [[ UIImageView alloc ] initWithImage :image];
cell. accessoryView = imageView;
// 打开 imageView 的用户交互
imageView. userInteractionEnabled = YES ;
// 为 imageView 添加一个手势
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tapAction:)];
[imageView addGestureRecognizer :tap];
[tap release ];
return cell;
}
// 实现打电话功能
- ( void )tapAction:( UITapGestureRecognizer *)sender {
// 获取点击的 cell
UITableViewCell *cell = ( UITableViewCell *)sender. view . superview ;
// 获取电话
//tel 打电话 sms 发短信
NSString *tel = [ NSString stringWithFormat : @"tel://%@" , cell. detailTextLabel . text ];
// 进行拨号操作 ( 该方法无法通过苹果审核,非苹果官方操作不能上传到 AppStore 上 )
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
// 苹果可以通过的审核的拨号操作 可以上传到 AppStore 上
self . phoneWebView = [[ UIWebView alloc ] initWithFrame : CGRectZero ];
if ( self . phoneWebView == nil ) {
self . phoneWebView = [[[ UIWebView alloc ] initWithFrame : CGRectZero ] autorelease ];
}
// 开始拨号
[ self . phoneWebView loadRequest :[ NSURLRequest requestWithURL :[ NSURL URLWithString :tel]]];
}
// 返回每一分区的内容
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section {
return self . keys [section];
}
// 展示索引条
- ( NSArray *)sectionIndexTitlesForTableView:( UITableView *)tableView {
return self . keys ;
}
#pragma mark SureEdite-DateSource
// 编辑 1. 确定分区的行是否可以编辑
- ( BOOL )tableView:( UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath {
if (indexPath. section < 2 ) {
return YES ;
} else {
return NO ;
}
}
// 编辑 2. 确定编辑的样式
- ( UITableViewCellEditingStyle )tableView:( UITableView *)tableView editingStyleForRowAtIndexPath:( NSIndexPath *)indexPath {
if (indexPath. section == 0 ) {
return UITableViewCellEditingStyleDelete ;
} else {
return UITableViewCellEditingStyleInsert ;
}
}
// 编辑 3. 设置编辑按钮显示的文字
- ( NSString *)tableView:( UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:( NSIndexPath *)indexPath {
return @" 确认删除 " ;
}
#pragma mark CommiteEdite-Datesource
// 编辑 4. 提交编辑操作
- ( void )tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete ) {
// 删除操作
// 取出对应分区的联系人列表
NSMutableArray *contacts = self . contectDic [ self . keys [indexPath. section ]];
if (contacts. count > 1 ) {
// 删除一个联系人
//1. 删除数据源中的数据
[contacts removeObjectAtIndex :indexPath. row ];
//2. 删除界面对应的数据
[ self . tableView deleteRowsAtIndexPaths : @[ indexPath ] withRowAnimation : UITableViewRowAnimationRight ];
} else {
// 删除整个分区
//1. 删除数据源中对应分区位置
// 获得对应分区的键
NSString *key = self . keys [indexPath. section ];
// 移除字典中对应的键值对
[ self . contectDic removeObjectForKey :key];
// 删除分区的标题
[ self . keys removeObject :key];
//2. 删除界面中对应分区位置
NSIndexSet *sectionSet = [ NSIndexSet indexSetWithIndex :indexPath. section ];
[ self . tableView deleteSections :sectionSet withRowAnimation : UITableViewRowAnimationRight ];
}
} else {
// 插入数据
NSDictionary *contact = @{ @"name" : @" 青菜叶 " , @"phone" : @"13103955005" , @"image" : @"caicai.png" } ;
// 插入到对应位置的数据源里面
// 获取指定的键 key 对应的联系人列表
NSMutableArray *contacts = self . contectDic [ self . keys [indexPath. section ]];
// 将联系人出插入到对应的联系人列表中
[contacts insertObject :contact atIndex :indexPath. row ];
//[self.contectDic[self.keys[indexPath.section]] insertObject:contact atIndex:indexPath.row];
// 将插入的数据展示到对应的界面上
[ self . tableView insertRowsAtIndexPaths : @[ indexPath ] withRowAnimation : UITableViewRowAnimationRight ];
}
}
#pragma mark Moving - Datesource
//cell 的移动 2. 设置可以移动的 cell
- ( BOOL )tableView:( UITableView *)tableView canMoveRowAtIndexPath:( NSIndexPath *)indexPath{
if (indexPath. section < 2 ) {
return YES ;
} else {
return NO ;
}
}
//cell 的移动 3. 设置 cell 移动的方向 ( 从一个位置移动到另外一个位置 )
- ( void )tableView:( UITableView *)tableView moveRowAtIndexPath:( NSIndexPath *)sourceIndexPath toIndexPath:( NSIndexPath *)destinationIndexPath {
// 移动数据源中的数据
// 获取对应分区的联系人列表
NSMutableArray *contacts = self . contectDic [ self . keys [sourceIndexPath. section ]];
// 取出要移动位置的联系人对象
NSDictionary *contact = [[contacts[sourceIndexPath. row ] retain ] autorelease ]; // 防止野指针的产生,你取出的联系人,要保证不被系统回收就 retain 一次。
// 删除对应位置的联系人对象
[contacts removeObjectAtIndex :sourceIndexPath. row ];
// 将要移动的联系人对象插入到想要插入的位置
[contacts insertObject :contact atIndex :destinationIndexPath. row ];
}
#pragma mark Moving - Delegating
//cell 的移动 4. 限定 cell 的移动范围
- ( NSIndexPath *)tableView:( UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:( NSIndexPath *)sourceIndexPath toProposedIndexPath:( NSIndexPath *)proposedDestinationIndexPath {
// 限定 cell 移动的范围只能在同一个分区下
if (sourceIndexPath. section == proposedDestinationIndexPath. section ) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
@end