#import "FYZAddressBookListController.h"
#import "FYZAddressBookDetailController.h"
#import "FYZAddAddressPersonController.h"
#import "FYZAddressBookNavigationController.h"
#import "AddressBookCell.h"
@interface FYZAddressBookListController ()<AddressBookCellDelegate>
@property (nonatomic, retain) UIWebView *callWebview;//拨打电话界面
@end
@implementation FYZAddressBookListController
//视图将要出现时触发,此时让tableView重新读一遍数据
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//让tableView重新刷新数据
[self.tableView reloadData];
}
//重写callWebview getter方法
- (UIWebView *)callWebview
{
if (!_callWebview) {
self.callWebview = [[[UIWebView alloc] init] autorelease];
//记得添加到view上
[self.view addSubview:_callWebview];
}
return [[_callWebview retain] autorelease];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.sectionIndexColor = [UIColor lightGreenColor];
// 配置navigationBar
[self customizeNavigationBarContent];
}
//定制navigationBar
- (void)customizeNavigationBarContent
{
//titleView
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
titleLabel.text = @"通讯录";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
RELEASE_SAFE(titleLabel);
//leftItem
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_contact"] style:UIBarButtonItemStylePlain target:self action:@selector(handleAdd:)];
self.navigationItem.leftBarButtonItem = leftItem;
RELEASE_SAFE(leftItem);
//右边的edit按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - handle action
- (void)handleAdd:(UIBarButtonItem *)item
{
/**
* 导航控制器用来管理一组具有层级关系的视图控制器,进入下一个界面的方式是push,返回是pop.
(层级关系:后一个界面显示的内容依赖于前一个界面)
当没有层级关系的视图控制器之间切换时(比如:前一个界面显示的内容依赖于后一个界面).要使用模态视图,进入下一个界面的方式是present,返回是dismiss.
*/
FYZAddAddressPersonController *addVC = [[FYZAddAddressPersonController alloc] init];
FYZAddressBookNavigationController *navigationVC = [[FYZAddressBookNavigationController alloc] initWithRootViewController:addVC];
//如果第二个界面需要导航控制器,则模态出导航控制器.
[self presentViewController:navigationVC animated:YES completion:nil];
RELEASE_SAFE(addVC);
RELEASE_SAFE(navigationVC);
// [self presentViewController:addVC animated:YES completion:^{
// //当视图模态出来之后,执行相应的操作.
// }];
NSLog(@"进入添加联系人界面");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - AddressBookCellDelegate
- (void)didClickCallButtonAtIndexPath:(NSIndexPath *)indexPath
{
//打电话
AddressPerson *person = [FYZAddressBookHelper addressPersonAtIndexPath:indexPath];
//打电话
NSString *telUrl = [NSString stringWithFormat:@"tel:%@",person.phoneNumber];
NSURL *telURL = [NSURL URLWithString:telUrl];
[self.callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [FYZAddressBookHelper numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [FYZAddressBookHelper numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"reuse";
AddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[AddressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.delegate = self;
}
cell.indexPath = indexPath;
//获取AddressPerson对象
cell.person = [FYZAddressBookHelper addressPersonAtIndexPath:indexPath];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [FYZAddressBookHelper titleForHeaderInSection:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [FYZAddressBookHelper sectionIndexTitles];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//删除分区还是删除一行
if ([FYZAddressBookHelper isNeedDeleteSection:indexPath.section]) {
//删除分区
//(1)移除数据源
[FYZAddressBookHelper deleteOneSection:indexPath.section];
//(2)删除界面上的分区
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:set withRowAnimation:UITableViewRowAnimationRight];
} else {
//删除一行
//(1)移除数据源
[FYZAddressBookHelper deleteRowAtIndexPath:indexPath];
//(2)删除对应的cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//修改数据源
[FYZAddressBookHelper moveAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取cell对应的AddressonPerson对象
AddressPerson *person = [FYZAddressBookHelper addressPersonAtIndexPath:indexPath];
//点击cell进入详情界面
FYZAddressBookDetailController *detailVC = [[FYZAddressBookDetailController alloc] init];
detailVC.person = person;
[self.navigationController pushViewController:detailVC animated:YES];
RELEASE_SAFE(detailVC);
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//限制在本区内移动
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}
return sourceIndexPath;
}
@end
通讯录列表界面
最新推荐文章于 2022-01-09 22:23:01 发布