PGzxc的博客

纸上得来终觉浅 绝知此事要躬行

0%

IOS开发之——私人通讯录自定义Cell和分割线(55)

一 概述

本文介绍自定义私人通讯录联系人界面的功能:

  • 自定义Cell
  • 设置分割线

二 效果图

三 功能实现

ContactCell.h

1
2
3
4
5
6
7

#import "Contact.h"
@interface ContactCell : UITableViewCell
@property (nonatomic,strong) Contact *contact;
+(instancetype)cellWithTableView:(UITableView *)tableView;

@end

ContactCell.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "ContactCell.h"
#import "Contact.h"

@interface ContactCell()
@property (nonatomic,weak) UIView *divide;
@end

@implementation ContactCell

- (UIView *)divide
{
if (_divide==nil) {
UIView *v=[[UIView alloc]init];
v.backgroundColor=[UIColor blackColor];
v.alpha=0.2;
_divide=v;
[self.contentView addSubview:_divide];
}
return _divide;
}
- (void)setContact:(Contact *)contact
{
_contact=contact;
//给cell的空间赋值
self.textLabel.text=contact.name;
self.detailTextLabel.text=contact.phone;
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID=@"contact";
return [tableView dequeueReusableCellWithIdentifier:ID];
}
//从XIB加载完成的时候
- (void)awakeFromNib {
[super awakeFromNib];
}
-(void)layoutSubviews
{
[super layoutSubviews];
//给分割线设置位置
CGFloat divideH=1;
CGFloat divideW=self.bounds.size.width;
CGFloat divideY=self.bounds.size.height-divideH;
self.divide.frame=CGRectMake(0, divideY, divideW, divideW);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}
@end