UItableViewCell 的自定義
在學(xué)習(xí)中檐薯,我們知道了cell的三種樣式凝赛,但是在實(shí)際編寫(xiě)中,很多時(shí)候我們需要用到不同的cell樣式坛缕,這時(shí)候我們就需要自定義cell墓猎。
- 首先,我們需要在父視圖上新建一個(gè)tableView
UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//tableview的代理方法祷膳。所以要在上邊遵守它的協(xié)議陶衅。
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 200.f;
[self.view addSubview:tableView];
UITableViewDelegate,
UITableViewDataSource
- 然后我們創(chuàng)建一個(gè)對(duì)象類(lèi),用于裝我們的對(duì)象的屬性直晨,
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *gender;
@property(nonatomic,retain)NSString *phoneNumber;
@property(nonatomic,retain)NSString *contactImageName;
- 然后我們需要在tableView的頁(yè)面引入頭視圖搀军,并且寫(xiě)一個(gè)獲取數(shù)據(jù)的方法,用于給對(duì)象賦值
-(void)getData {
//創(chuàng)建聯(lián)系人對(duì)象勇皇,加到數(shù)組中
Contact *contact1 = [[Contact alloc] init];
contact1.name = @"小明";
contact1.gender = @"男";
contact1.phoneNumber = @"121212";
contact1.contactImageName = @"1.jpg";
Contact *contact2 = [[Contact alloc] init];
contact2.name = @"小紅";
contact2.gender = @"女";
contact2.phoneNumber = @"121212123";
contact2.contactImageName = @"3.jpg";
self.contactArray = @[contact1,contact2];
}
*不要忘了tableView的兩個(gè)必須實(shí)現(xiàn)的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contactArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//從數(shù)組取出的對(duì)象
Contact *contact = self.contactArray[indexPath.row];
//static 只初始化一次
static NSString *contactIdentifier = @"contact";
//用我們自定義的cell罩句,
ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:contactIdentifier];
if (nil == cell) {
cell = [[ContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactIdentifier];
}
//左邊的contact 是cell中的,右邊的是從數(shù)組中取出的聯(lián)系人對(duì)象
cell.contact = contact;
return cell;
}
- 接下來(lái)是自定義cell的部分敛摘,我們需要新建一個(gè)cell類(lèi)门烂,繼承于UItableViewCell。
- 定義要顯示在cell上的控件的屬性,
@interface ContactCell ()
@property(nonatomic,retain)UIImageView * contactImageView;
@property(nonatomic,retain)UILabel * nameLable;
@property(nonatomic,retain)UILabel * genderLable;
@property(nonatomic,retain)UILabel * phoneNumberLable;
@end
- 然后是重寫(xiě)cell的方法:
//重寫(xiě)cell的方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contactImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.contactImageView.backgroundColor = [UIColor redColor];
//contentView是cell自帶的view屯远,
[self.contentView addSubview:self.contactImageView];
self.nameLable = [[UILabel alloc] initWithFrame:CGRectZero];
self.nameLable.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:self.nameLable];
self.genderLable = [[UILabel alloc] initWithFrame:CGRectZero];
self.genderLable.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:self.genderLable];
self.phoneNumberLable = [[UILabel alloc] initWithFrame:CGRectZero];
self.phoneNumberLable.backgroundColor = [UIColor orangeColor];
[self.contentView addSubview:self.phoneNumberLable];
}
return self;
}
- cell的布局蔓姚,防止以后我們需要進(jìn)行改動(dòng),我們可以把cell上控件的布局寫(xiě)在下面的方法里;
//在這個(gè)方法寫(xiě)子視圖的布局代碼
-(void)layoutSubviews {
//調(diào)用父類(lèi)的方法
[super layoutSubviews];
self.contactImageView.frame = CGRectMake(10, 10, 100, 120);
self.nameLable.frame = CGRectMake(self.contactImageView.frame.origin.x + self.contactImageView.frame.size.width + 10, self.contactImageView.frame.origin.y, 100, 30);
CGRect genderFrame = self.nameLable.frame;
//調(diào)整genderFrame的y值
genderFrame.origin.y = self.nameLable.frame.origin.y + self.nameLable.frame.size.height + 5;
//最后把調(diào)整好的frame賦給genderLable的frame
self.genderLable.frame = genderFrame;
CGRect phoneNumberFrame = genderFrame;
phoneNumberFrame.origin.y = genderFrame.origin.y + genderFrame.size.height + 5;
self.phoneNumberLable.frame = phoneNumberFrame;
}
- 在賦值的時(shí)候慨丐,我們需要重寫(xiě)setter方法
//重寫(xiě)setter方法坡脐。
-(void)setContact:(Contact *)contact {
if (_contact != contact) {
_contact = contact;
//給視圖賦值,
self.nameLable.text = contact.name;
self.genderLable.text = contact.gender;
self.phoneNumberLable.text = contact.phoneNumber;
self.contactImageView.image = [UIImage imageNamed:contact.contactImageName];
}
//注意:這里不能寫(xiě)成 self.contact = contact; 因?yàn)檫@樣會(huì)無(wú)限的調(diào)用自己房揭,叫做遞歸备闲;
}