通讯录列表界面

本文介绍了如何在iOS中实现通讯录列表界面,包括使用UITableView显示数据,自定义单元格,以及实现添加、编辑和删除联系人的功能。还涉及到了导航栏的定制,以及通过UIWebView实现电话拨打。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值