TableView高階(自定義cell、cell自定義高度)
一、MVC,一種框架級的軟體設計模式
(1)名稱解釋
"Model"資料模型封裝了APP的資料
"View"檢視 UIView以及它的子類,處理使用者操作(大部分都是通過委託的方式交給控制器處理),顯示資料
"Controll"控制器負責Model和View的通訊,處理業務通訊
(2)操作:分別建立三個分組:"Model、View、Controll"
(3)特點:
1.提高程式碼的複用性
2.提高程式碼的可讀性
3."高內聚,低耦合",使功能更加模組化
二、自定義cell:(UITableViewCell)
1.新建model類,處理資料,處理成model物件
2.cell有的控制元件宣告成自定義cell的屬性,在cell的初始化方法中初始化屬性,不設定frame
3.給cell新增model屬性,重寫setter方法,給控制元件賦值
4.再去重寫layoutSubview方法,設定控制元件的frame
5.在協議方法中自定義cell,給cell的model屬性賦值
1.新建model類(學生類),處理資料
2.cell初始化自身屬性,自定義cell(MyView)
//初始化自身的同時初始化屬性
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//在建立的時候不知道行高
if (self) {
self.nameLabel = [[UILabel alloc]init];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
self.nameLabel.backgroundColor = [UIColor yellowColor];
//不加在self上
[self.contentView addSubview:self.nameLabel];
}
returnself;
}
3.給cell新增model屬性,重寫setter方法,給控制元件賦值
- (void)setStu:(Student *)stu{
_stu = stu;
self.nameLabel.text = stu.name;
self.ageLabel.text = [stu.age stringValue];
self.phoneNumberLabel.text = stu.phoneNumber;
self.sexLabel.text = stu.sex;
}
4.重寫layoutSubview方法,設定控制元件的frame
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat width =self.bounds.size.width;
CGFloat height =self.bounds.size.height;
self.label.frame = CGRectMake(10,10, width -20, height -20);
}
5.在協議方法中自定義cell,給cell的model屬性賦值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusepool"];
if (cell ==nil) {
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reusepool"];
}
//注意這裡是cell.label(自定義cell的屬性)
cell.label.text =self.strArr[indexPath.row];
return cell;
}
三、tableView使用不同型別的cell:在返回cell的協議方法中,根據model物件屬性的值判斷使用哪種cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Student *stu =self.stuArr[indexPath.row];
//根據本行顯示的Model物件,判斷使用哪種cell, 不同的重用池
if ([stu.sex isEqualToString:@"男"]) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusepool"];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reusepool"];
}
cell.stu = stu;
return cell;
}
else{
WomanCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
if (cell == nil) {
cell = [[WomanCustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"];
}
cell.stu = stu;
return cell;
}
}
四、cell自定義高度封裝工具類
//在cell中定義一個label並在label中顯示文字
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//獲取本行文字
NSString *str =self.strArr[indexPath.row];
//根據字串算出完全顯示需要多大
//引數1: CGSizeMake(345, 0)//label的寬度
//引數3: [UIFont systemFontOfSize:17]保證字型一致
//引數4: context上下文
//CGRect rect = [str boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//可以將計算高度封裝成一個工具類
CGFloat rectHeight = [CalculateHeight heightWithString:str labelWidth:tableView.bounds.size.width -30 font:17];
//計算cell應用的高度(label的寬高用self.contentView.bounds.size.height需要加0.5)
CGFloat height = rectHeight +20;//20是label與cell的大小差距
return height;
}