UItableViewCell 的自定義
在學習中,我們知道了cell的三種樣式,但是在實際編寫中,很多時候我們需要用到不同的cell樣式,這時候我們就需要自定義cell。
- 首先,我們需要在父視圖上新建一個tableView
UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//tableview的代理方法。所以要在上邊遵守它的協議。
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 200.f;
[self.view addSubview:tableView];
UITableViewDelegate,
UITableViewDataSource
- 然后我們創建一個對象類,用于裝我們的對象的屬性,
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *gender;
@property(nonatomic,retain)NSString *phoneNumber;
@property(nonatomic,retain)NSString *contactImageName;
- 然后我們需要在tableView的頁面引入頭視圖,并且寫一個獲取數據的方法,用于給對象賦值
-(void)getData {
//創建聯系人對象,加到數組中
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的兩個必須實現的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contactArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//從數組取出的對象
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中的,右邊的是從數組中取出的聯系人對象
cell.contact = contact;
return cell;
}
- 接下來是自定義cell的部分,我們需要新建一個cell類,繼承于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
- 然后是重寫cell的方法:
//重寫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的布局,防止以后我們需要進行改動,我們可以把cell上控件的布局寫在下面的方法里;
//在這個方法寫子視圖的布局代碼
-(void)layoutSubviews {
//調用父類的方法
[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;
//調整genderFrame的y值
genderFrame.origin.y = self.nameLable.frame.origin.y + self.nameLable.frame.size.height + 5;
//最后把調整好的frame賦給genderLable的frame
self.genderLable.frame = genderFrame;
CGRect phoneNumberFrame = genderFrame;
phoneNumberFrame.origin.y = genderFrame.origin.y + genderFrame.size.height + 5;
self.phoneNumberLable.frame = phoneNumberFrame;
}
- 在賦值的時候,我們需要重寫setter方法
//重寫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];
}
//注意:這里不能寫成 self.contact = contact; 因為這樣會無限的調用自己,叫做遞歸;
}