创建通讯录及其详情页面

来源:互联网 发布:睡衣 知乎 编辑:程序博客网 时间:2024/06/16 15:29

这次选择的是MVC写法.

首先创建一个空工程,然后创建Model,TableViewCell和ViewController


创建好文件后,开始写代码

将工程改为了手动内存管理

首先AppDelegate.m,将导航视图控制器设置为根视图

    AddressbookViewController *addressVC = [[AddressbookViewController alloc] init];    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:addressVC];    self.window.rootViewController = nav;    [addressVC release];    [nav release];



addressBookModel中的代码如下:

在.h文件中将通讯录中成员的基本信息:姓名,年龄等设置为属性,然后定义初始化方法

@interface addressbookModel : NSObject@property(nonatomic, retain) NSString *name;@property(nonatomic, retain) NSString *gender;@property(nonatomic, retain) NSString *phoneNum;@property(nonatomic, retain) NSString *age;@property(nonatomic, retain) NSString *head_image;@property(nonatomic, retain) NSString *hobby;//初始化方法//- (instancetype)initWithDictionary:(NSDictionary *)dic;


这里的初始化方法是一般的初始化方法,我试验了一下在初始化方法里加入block

- (void)getModelFromDictionary:(NSDictionary *)dic andModelBlock:(void(^)(addressbookModel *addressModel))addressBookBlock;

这两种方法结果并没有不同,只是另一种尝试罢了,后者较前者麻烦,所以平时并不使用


.m文件代码

#import "addressbookModel.h"@implementation addressbookModel//自定义初始化,把传过来的字典解析成model数据类型- (instancetype)initWithDictionary:(NSDictionary *)dic{    self = [super init];    if (self) {        self.name = dic[@"name"];        self.gender = dic[@"gender"];        self.head_image = dic[@"head_image"];        self.hobby = dic[@"hobby"];        self.age = dic[@"age"];        self.phoneNum = dic[@"phoneNum"];    }    return self;}- (void)getModelFromDictionary:(NSDictionary *)dic andModelBlock:(void (^)(addressbookModel *))addressBookBlock{    addressbookModel *addressModel = [[addressbookModel alloc] initWithDictionary:dic];    //把转化完成的block回传给controller    addressBookBlock(addressModel);}@end


如果不用block,那初始化完成之后,下面就不用写


MVC中的M写完了,就该V了

addressbookTableViewCell.h代码

先引入头文件,然后定义一个Model属性

#import <UIKit/UIKit.h>#import "addressbookModel.h"@interface addressbookTableViewCell : UITableViewCell@property(nonatomic, retain) addressbookModel *model;@end


addressbookTableViewCell.m

#import "addressbookTableViewCell.h"//屏幕宽度  把屏幕宽度定义成宏定义,在设置cell的时候用屏幕宽度,可以让cell适应不同的屏幕大小#define kWidth [UIScreen mainScreen].bounds.size.width@interface addressbookTableViewCell ()//头像@property(nonatomic, retain)UIImageView *headImageView;//姓名@property(nonatomic, retain)UILabel *namelabel;//电话@property(nonatomic, retain)UILabel *phonenumlabel;@end@implementation addressbookTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        //加载视图        [self loadingCustomView];    }    return self;}//释放-(void)dealloc{    [_headImageView release],  _headImageView = nil;;    [_namelabel release],      _namelabel = nil;;    [_phonenumlabel release],  _phonenumlabel = nil;;    [super dealloc];            }- (void)loadingCustomView{    //懒加载    [self addSubview:self.headImageView];    [self addSubview:self.namelabel];    [self addSubview:self.phonenumlabel];        [self.namelabel release];    [self.phonenumlabel release];    [self.namelabel release];    }//需要一个imageView和两个lable - (UIImageView *)headImageView{    if (_headImageView == nil) {        self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 60, 60)];        self.headImageView.layer.cornerRadius = self.headImageView.frame.size.width / 2;        self.headImageView.clipsToBounds = YES;     }    return _headImageView;}- (UILabel *)namelabel{    if (_namelabel == nil) {        self.namelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, kWidth -  80, 35)];            }    return _namelabel;}- (UILabel *)phonenumlabel{    if (_phonenumlabel == nil) {        self.phonenumlabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, kWidth -  80, 40)];    }    return _phonenumlabel;}//重写model的setter方法- (void)setModel:(addressbookModel *)model{    self.namelabel.text = model.name;    self.phonenumlabel.text = model.phoneNum;    self.headImageView.image = [UIImage imageNamed:model.head_image];    }



AddressbookViewController.h

写入两个代理

#import <UIKit/UIKit.h>@interface AddressbookViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>@end

AddressbookViewController.m

#import "AddressbookViewController.h"//(5)#import "addressbookTableViewCell.h"#import "addressbookModel.h"#import "DetailViewController.h"@interface AddressbookViewController ()@property (nonatomic, retain) UITableView *tableView;@property(nonatomic, retain) NSArray *sectionTitleArray;@property(nonatomic, retain) NSMutableArray *allPeopleArray;@end@implementation AddressbookViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.         self.title = @"通讯录";        //创建联系人列表        //lazy loading 懒加载(就是先不创建,用的时候再去创建)        [self.view addSubview:self.tableView];     //解析数据    [self configTableData];   }- (void)configTableData{    //查找plist文件在工程中的路径    NSString *path = [[NSBundle mainBundle]pathForResource:@"AddressBook" ofType:@".plist"];    //通过路径,把plist文件转化成字典类型的数据    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];        //分区标题    self.sectionTitleArray = dic.allKeys;    self.allPeopleArray = [NSMutableArray new];    for (NSString *str in self.sectionTitleArray) {        //定义一个小组,存放每组人员的model数据        __block NSMutableArray *group = [NSMutableArray new];        NSArray *array = dic[str];        for (NSDictionary *dic in array) {            addressbookModel *model = [[addressbookModel alloc] init];            [model getModelFromDictionary:dic andModelBlock:^(addressbookModel *addressModel) {                [group addObject:addressModel];            }];         }        [self.allPeopleArray addObject:group];    }}//实现代理- (UITableView *)tableView{//先判断实例变量是否存在    if (_tableView == nil) {        //若不存在就去创建一个       self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];        self.tableView.separatorColor = [UIColor blackColor];                //设置背景图片                self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"head_06.jpg"]];        self.tableView.dataSource = self;        self.tableView.delegate = self;        self.tableView.rowHeight = 80;    }    return _tableView;}#pragma mark ------- UITableViewDataSource//(4)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSArray *group = self.allPeopleArray[section];    return group.count;}//(6)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //定义重用标示符    static NSString *cellIdentifier = @"cellIdentifier";    //从重用队列取出cell    addressbookTableViewCell *addresscell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    //如果为空则重新创建    if (addresscell == nil) {        addresscell = [[addressbookTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];    }    NSMutableArray *group = self.allPeopleArray[indexPath.section];    addresscell.model = group[indexPath.row];    //背景颜色设置为透明色    //addresscell.backgroundColor = [UIColor clearColor];    return addresscell;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return self.sectionTitleArray[section];}#pragma mark------UITableViewDelegate- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.sectionTitleArray.count;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //详细信息页面,把单个点击的联系人的model传过去    DetailViewController *deVC = [[DetailViewController alloc] init];    NSMutableArray *group = self.allPeopleArray[indexPath.section];    deVC.model = group[indexPath.row];    [self.navigationController pushViewController:deVC animated:YES];}
这时候,通讯录页面就创建好了

接下来创建个人详细信息页面,当点中某个人的时候会出现

DetailViewController.h

定义一个model,通过model把上一个页面成员的详细信息传过来

#import <UIKit/UIKit.h>#import "addressbookModel.h"@interface DetailViewController : UIViewController@property(nonatomic, retain) addressbookModel *model;@end




DetailViewController.m

@interface DetailViewController ()@end@implementation DetailViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];    self.title = self.model.name;        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 80, KWdith - 160, KWdith - 160)];    imageView.image = [UIImage imageNamed:self.model.head_image];    imageView.layer.cornerRadius = (KWdith - 160) / 2;    imageView.clipsToBounds = YES;    [self.view addSubview:imageView];    [imageView release];    //姓名    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(80, KWdith - 160 + 80, KWdith - 160, 44)];    nameLable.text = [NSString stringWithFormat:@"姓名:  %@", self.model.name];    [self.view addSubview:nameLable];    [nameLable release];    //年龄    UILabel *ageLable = [[UILabel alloc] initWithFrame:CGRectMake(80, KWdith - 160 + 80 + 44, KWdith - 160, 44)];    ageLable.text = [NSString stringWithFormat:@"年龄:  %@", self.model.age];    [self.view addSubview:ageLable];    [ageLable release];    //性别    UILabel *sexLable = [[UILabel alloc] initWithFrame:CGRectMake(80, KWdith - 160 + 80 + 88, KWdith - 160, 44)];    sexLable.text = [NSString stringWithFormat:@"性别:  %@", self.model.gender];    [self.view addSubview:sexLable];    [sexLable release];    //手机号    UILabel *numLable = [[UILabel alloc] initWithFrame:CGRectMake(80, KWdith - 160 + 80 + 44 + 88, KWdith - 160, 44)];    numLable.text = [NSString stringWithFormat:@"手机号: %@", self.model.phoneNum];    [self.view addSubview:numLable];    [numLable release];    //爱好    UILabel *hobbyLable = [[UILabel alloc] initWithFrame:CGRectMake(80, KWdith - 160 + 80 + 88 + 88, KWdith - 160, 44)];    hobbyLable.text = [NSString stringWithFormat:@"爱好:  %@", self.model.hobby];    [self.view addSubview:hobbyLable];    [hobbyLable release];    }
这样简单的通讯录就做好了,不过并不能添加新联系人,以后有空再做一个吧

0 0
原创粉丝点击