##DAY12 UITableViewCell自定義佈局
#pragma mark -------自定義視圖步驟---------字體
自定義視圖步驟:ip
1)在自定義cell類中,將全部cell要顯示的子視圖控件都聲明成屬性內存
2)重寫cell的初始化方法,對內部控件進行佈局,frame指定爲0(CGRectZero),將控件添加到cell上面進行顯示,必定要注意使用self.contentView添加;string
//自定義cell內部添加子視圖,不能使用self,應該使用self.contentViewit
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier內存管理
3)在cell內部,重寫layoutSubViews方法(先向父類的此方法發送消息),給定內部控件的具體位置;io
4)創建模型類,設置屬性、異常處理;table
//內部什麼都不作,異常處理,解決賦值個數不匹配的問題queue
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if ([key isEqualToString:@"id"]) {
self.ID = value;
}
if ([key isEqualToString:@"description"]) {
self.descriptions = value;
}
}
5)在cell內部導入模型,將模型設置成屬性;
6)在cell內部,重寫模型屬性的setter方法,內部使用模型爲內部控件完成賦值;
//在cell內部綁定一個模型屬性
//重寫模型的setter方法,完成賦值
- (void)setStudent:(Student *)student{
if(_student != student){
[_student release];
_student = [student retain];
}
//爲內部控件進行賦值,若是寫在裏面,當數據相同時,第二個cell就沒有被賦值
_headerImageView.image = [UIImage imageNamed:_student.picture];
_nameLabel.text = _student.name;
_genderLabel.text = _student.gender;
_ageLabel.text = _student.age;
}
7)內存管理,自定義cell類,模型類中釋放屬性。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"reuse";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
}
Student *student = _dataArr[indexPath.row];
cell.student = student;//簡化
return cell;
}
#pragma mark ———cell自適應高度———
#define kWidth [[UIScreen mainScreen] bounds].size.width//宏定義會直接替換,類方法中不能使用self.view
#define kImageWidth ((kWidth - 30) / 4)
//求一段文本的顯示高度
+ (CGFloat)heightForString:(NSString *)string {
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
//下面的方法會根據參考寬度和字體的size計算出一個寬度返回出去,這裏的CGSizeMake()中的兩個參數,第一個是參考寬度,;第二個參數是返回的最大高度
//kImageWidth 即 (([[UIScreen mainScreen] bounds].size.width-30)/4)宏定義會直接替換,類方法中不能使用self.view
return [string boundingRectWithSize:CGSizeMake(3 * kImageWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:diccontext:nil].size.height;
}
//返回cell的高度
+ (CGFloat)cellHeightForStudent:(Student *)student {
return 65 + [BoyTableViewCell heightForString:student.introduce] > 120 ? 65 + [BoyTableViewCell heightForString:student.introduce] : 120 ;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat imageWidth = (kWidth - 30) / 4;
_headerImageView.frame = CGRectMake(10, 5, imageWidth, 110);
_introduceLabel.frame = CGRectMake(20 + imageWidth, 65, 3 * imageWidth, [BoyTableViewCell heightForString:_introduceLabel.text]);
}
#pragma mark ------MyTableViewController.m-------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
Student *stu = _dataArr[indexPath.row];
if ([stu.sex isEqualToString:@"男"]) {
return [BoyTableViewCell cellHeightForStudent:stu];
}else if([stu.sex isEqualToString:@"女"]){
return [GirlTableViewCell cellHeightForStudent:stu];
}
return 270;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}