iOS入门-公司通讯录项目(教你一步步完成一个公司通讯录)

刚刚入公司,应公司要求,分享一个简单的iOS项目开发过程,面向人员主要为:没有iOS开发经验的员工。

在经过一番查看后,发现手头正有公司的通讯录名单(Excel格式的),决定直接做一款公司通讯录

其中涉及到的iOS开发知识如下:

1Xcode的基本使用

2iOS程序启动运行过程

3UITableView的使用

4、导航控制器的使用

5、调用系统打电话、发短信、发邮件功能


下面正式开始开发过程

详细步骤:

xx公司通讯录iOS客户端编码详解

第一步:新建项目 打开Xcode,选择新建项目(Createa new Xcode project),选择iOS项目,Empty Application

 



第二步:添加资源文件(直接拖动资源文件至项目中)


注意勾选以下选项(保证项目拷贝其他地方扔然可用)


第三步:新建项目文件

新建TableView控制器




新建数据模型


新建联系人详情控制器:(演示代码暂不实现,源码中已经实现,有兴趣请看源码)

 

 

 

 

第四步:代码编写

1、启动基础代码新增,修改项目HMAppDelegate.m文件,增加项目启动代码


    // 1.创建窗口

    self.window = [[UIWindowalloc] init];

    self.window.frame = [UIScreenmainScreen].bounds;

   

    // 2.显示窗口(成为主窗口)

    [self.windowmakeKeyAndVisible];

   

    // 3.添加导航控制器,导航控制器的主控制器设置为TableViewController

    HMAddressBookTableViewController *addTableViewCon = [[HMAddressBookTableViewControlleralloc] initWithStyle:UITableViewStylePlain];

    UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:addTableViewCon];

    addTableViewCon.title =@"通讯录";

self.window.rootViewController = nav;

添加完毕后,运行(cmd+R),运行效果如下:


2、模型数据修改

2-1在HMMember.h文件中定义相应的变量:

@property (nonatomic,strong)NSString *department;

@property (nonatomic,strong)NSString *eMail;

@property (nonatomic,strong)NSString *employeeID;

@property (nonatomic,strong)NSString *enName;

@property (nonatomic,strong)NSString *name;

@property (nonatomic,strong)NSString *position;

@property (nonatomic,strong)NSString *sex;

@property (nonatomic,strong)NSString *telNum;

@property (nonatomic,strong)NSString *telNum1;

@property (nonatomic,strong)NSString *weixinNum;

 

- (instancetype) initWithDict:(NSDictionary *)dict;

+ (instancetype)initWithDict:(NSDictionary *)dict;

 

 

2-2在HMMember.m文件中实现类方法

+ (instancetype)initWithDict:(NSDictionary *)dict

{

    return [[self alloc] initWithDict:dict];

}

 

- (instancetype)initWithDict:(NSDictionary *)dict

{

    if(self = [superinit])

    {

        [selfsetValuesForKeysWithDictionary:dict];

    }

    return self;

}

 

 

 

3、TableView控制器代码编写

 

3-1设置HMAddressBookTableViewController遵守TalbView的代理和数据协议:

@interface HMAddressBookTableViewController ()<UITableViewDelegate,UITableViewDataSource>

3-2设置代理和数据为self

在ViewDidLoad中添加如下代码

         self.tableView.delegate   =self;

self.tableView.dataSource =self;

3-3添加全局变量members存放所有的通讯录人员数据

@property (nonatomic,strong)NSArray *members;

3-4重写_members的set方法

    - (NSArray *)members

{

    if (_members ==nil) {

        NSString *path = [[NSBundlemainBundle] pathForResource:@"txl2"ofType:@"plist"];

        NSArray *arr = [NSArrayarrayWithContentsOfFile:path];

        _members = [NSMutableArrayarrayWithArray:arr];

    }

    return_members;

}

3-5实现tableview的代理方法

a)      分组数量设置

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 

    returnself.members.count;

}

b)     分组名称设置

 - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section

{

    NSDictionary *dir =self.members[section][0];

    return dir[@"department"];

}

c)      每个分组数量设置

 - (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section

{

    NSArray *membersOfDepartment =self.members[section];

    return membersOfDepartment.count;

}

d)     每个Cell的内容设置

 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    staticNSString *ID = @"raiyee";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID ];

   

    if (cell ==nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];

        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    }

   

    NSDictionary *dir =self.members[indexPath.section][indexPath.row];

    HMMember *member = [HMMemberinitWithDict:dir];

    cell.textLabel.text = member.name;

    cell.detailTextLabel.text = member.telNum;

    cell.detailTextLabel.textColor = [UIColorgrayColor];

    return cell;

 

}

运行效果:


 

ps:

考虑演示,简单易懂的原则

1、很多地方没有涉及比较复杂的实现

2、通讯录数据暂时放本地,未使用网络数据加载的方式

3、部分代码未严格安装编程规范来编写

4、联系人详情页面展示未放在演示中讲解(在代码中已包含)

5、由于联系人涉及公司隐私,故github代码中通讯录文件已替换为测试数据


放两张效果图,大家随便感受一下:




代码暂只演示这些,供大家了解基本使用过程,源码已放置GitHub

源码地址: https://github.com/gaohm/AddressBook
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水月天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值