您的位置:首页 > 产品设计 > UI/UE

UI:用UITableView制作通讯录的关键代码

2015-09-14 08:48 323 查看
分析{功能分析(打电话、添加联系人、修改联系人),模块分析(联系人展示、详情模块、添加模块)}

拿到一个项目,首先分析项目框架(工程框架)

首先:判断是否是用户第一次安装:(如果是的,那就加载用户引导页面)(如果不是,那就显示用户联系人的主页面)

其次:用户联系人的主页面VC(用一个协助类 addressbookheleper类(做数据处理相关的内容,属于MVC的M层。只处理相关的数据,这里是为VC瘦身)去提供相应的方法 给 controller (ContactListViewController) )这里面有这几个方法:为VC提供tableViewCell某一分区得到行数、分区个数、对应的分区的标题、右侧的索引、是否要删除某分区、是否要删除某一行、移动某分区的某一行、返回对应行的联系人、添加联系人。

#import <UIKit/UIKit.h>
@class Contact;

//为 callBtn 制订协议,实现拨打电话
@protocol MakeACallDelegate <NSObject>

-(void)dial:(NSIndexPath *)indexPath;

@end

@interface CustomContactCellTabviewCell : UITableViewCell
@property (nonatomic ,retain) UIImageView *photoView;
@property (nonatomic ,retain) UILabel *nameLabel;
@property (nonatomic ,retain) UILabel *phoneLabel;
@property (nonatomic ,retain) UIButton *callBtn;
@property(nonatomic,assign)id<MakeACallDelegate>delegate;
@property(nonatomic,retain)NSIndexPath * indexPath;//存储当前点击 call 的索引
@property(nonatomic,retain)Contact * contact;//声明一个联系人对象的属性
//为cell 上的控件赋值
-(void)configureCell:(Contact *)contact;
@end

//
//  CustomContactCellTabviewCell.m

#import "CustomContactCellTabviewCell.h"
#import "Contact.h"
#import "AddressBookHelper.h"

@implementation CustomContactCellTabviewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.photoView];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.phoneLabel];
[self.contentView addSubview:self.callBtn];
}

return self;
}

#pragma lazy loading 实现控件的创建
- (UIImageView *)photoView {
if (!_photoView) {
self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
_photoView.layer.cornerRadius = 5.0;
_photoView.layer.masksToBounds = YES;
}

return [[_photoView retain]autorelease];

}
- (UILabel *)nameLabel {
if (!_nameLabel) {
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, self.frame.size.width / 3, 40)];
}
return [[_nameLabel retain]autorelease];

}
- (UILabel *)phoneLabel {
if (!_phoneLabel) {
self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(70 + self.frame.size.width / 3, 30, self.frame.size.width / 2, 40)];
}
return [[_phoneLabel retain]autorelease];

}
- (UIButton *)callBtn {
if (!_callBtn) {
self.callBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_callBtn.frame = CGRectMake(self.frame.size.width - 20, 30, 40, 40);
//添加触发
[_callBtn addTarget:self action:@selector(handleCall:) forControlEvents:UIControlEventTouchUpInside];
[_callBtn setImage:[UIImage imageNamed:@"action_call"] forState:UIControlStateNormal];
}
return _callBtn;
}
#pragma mark---打电话按钮
- (void)handleCall:(UIButton *)sender {
if (!_delegate && [_delegate respondsToSelector:@selector(dial:)]) {
[_delegate dial:self.indexPath];
}
}

- (void)dealloc {
self.nameLabel = nil;
self.photoView = nil;
self.phoneLabel = nil;
self.callBtn = nil;
[super dealloc];
}

- (void)awakeFromNib {
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

//第一种 : 自定义方法实现对 cell 的控件的赋值
-(void)configureCell:(Contact *)contact{
self.nameLabel.text = contact.name;
self.phoneLabel.text = contact.phoneNum;
self.photoView.image = [UIImage imageNamed:contact.photo];

}

//第二种 :重写 setter 方法 为 cell 上的方法赋值
/*
-(void)setContact:(Contact *)contact{
if (_contact != contact) {
[_contact release];
_contact = [contact retain];
}
self.nameLabel.text = contact.name;
self.phoneLabel.text = contact.phoneNum;
self.photoView.image = [UIImage imageNamed:contact.photo];

}
*/

@end


View Code contactContactCelltableCell.h & .m 文件

其他参考:参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: