//
// RootViewController.m
#import "RootViewController.h"
#import "Student.h"
#import "MyTableViewCell.h"
#import "SecTableViewCell.h"
#import "AjustFrame.h"
@interfaceRootViewController()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView*tableView;
@property (nonatomic,strong)NSMutableArray*dataArr;
@end
@implementationRootViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.tableView =[[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStyleGrouped];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.viewaddSubview:self.tableView];
}
// 懒加载即重写了dataArr属性的getter方法。
//懒加载,只有调用了getter方法的时候才会加载数据,能够减少viewDidLoad的负担,减少内存消耗
- (NSMutableArray *)dataArr{
if (_dataArr == nil) {
// 创建数组,用来存储学生对象
self.dataArr = [NSMutableArray array];
// 获取Students.plist文件
NSString *file =[[NSBundlemainBundle]pathForResource:@"Students"ofType:@"plist"];
// 获取文件中的数据
NSMutableArray *arr =[NSMutableArrayarrayWithContentsOfFile:file];
// 将数据和model类联系起来
// 数据解析
for (NSDictionary *dic in arr) {
// 定义一个Student对象
Student *stu = [[Student alloc]init];
// 将学生与字典对应
[stusetValuesForKeysWithDictionary:dic];
// 将学生存进数组
[self.dataArraddObject:stu];
}
}
return _dataArr;
}
// 获取数据,通过 [selfgetData] 使用
//- (void)getData{
// // 创建数组
// self.dataArr = [NSMutableArray array];
// // 获取Students.plist文件
// NSString *file = [[NSBundlemainBundle]pathForResource:@"Students"ofType:@"plist"];
// // 获取文件中的数据
// NSMutableArray *arr = [NSMutableArrayarrayWithContentsOfFile:file];
// // 将数据和model类联系起来
// // 数据解析
// for (NSDictionary *dic in arr) {
// // 定义一个Student对象
// Student *stu = [[Student alloc]init];
// // 将学生与字典对应
// [stusetValuesForKeysWithDictionary:dic];
// // 将学生存进数组
// [self.dataArraddObject:stu];
// }
//
//}
- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
return 7;
}
// 从重用列表中获取cell或者重新创建一个cell
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 首先得到学生
Student *stu = self.dataArr[indexPath.row];
// 判断学生性别决定使用哪种自定义cell
if ([stu.sexisEqualToString:@"男"]) {
MyTableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"reuse"];
if (cell == nil) {
cell =[[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"reuse"];
}
Student *student = self.dataArr[indexPath.row];
cell.stu = student;
return cell;
}else{
SecTableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"log"];
if (cell == nil) {
cell =[[SecTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"log"];
}
Student *student = self.dataArr[indexPath.row];
cell.student = student;
return cell;
}
}
// 计算cell的高度
- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
// // 得出对应单元格的学生
// Student *stu = self.dataArr[indexPath.row];
// // 得出字符串的文本属性
// NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
// // 计算字符串所占高度(label的高度)
// CGFloat height = [stu.introduceboundingRectWithSize:CGSizeMake(200,1000)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size.height;
// // cell的高度就是字符串的高度加上label的高度
// return height + 130;
// 自适应高度,根据cell中内容的多少设置高度
// 获取学生对象
Student *stu = self.dataArr[indexPath.row];
// 计算学生对象中字符串stu.introduce的内容高度
CGFloat height =[AjustFrameheightForString:stu.introduce width:200 Font:[UIFontsystemFontOfSize:17]];
return height + 110;
}
@end
//
// SecTableViewCell.m
#import "SecTableViewCell.h"
#import "AjustFrame.h"
@implementationSecTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
[supersetSelected:selectedanimated:animated];
}
// 自定义的cell
-(instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{
self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) {
self.imageV =[[UIImageViewalloc]initWithFrame:CGRectMake(250, 5, 150, 180)];
[self.contentViewaddSubview:self.imageV];
self.nameLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 5, 200, 30)];
[self.contentViewaddSubview:self.nameLabel];
self.phoneNumberLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 40, 200, 30)];
[self.contentViewaddSubview:self.phoneNumberLabel];
self.sexLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 75, 200, 30)];
[self.contentViewaddSubview:self.sexLabel];
self.introduceLabel =[[UILabelalloc]initWithFrame:CGRectMake(5, 110, 200, 30)];
self.introduceLabel.numberOfLines = 0;
[self.contentViewaddSubview:self.introduceLabel];
}
return self;
}
- (void)setStudent:(Student *)student{
self.imageV.image =[UIImageimageNamed:student.icon];
self.nameLabel.text = student.name;
self.phoneNumberLabel.text =student.phoneNumber;
self.sexLabel.text = student.sex;
self.introduceLabel.text =student.introduce;
// 获取introduceLabel中字符串的高度
CGFloat height =[AjustFrameheightForString:self.introduceLabel.text width:200Font:self.introduceLabel.font];
// 重新设置introducelabel的大小
self.introduceLabel.frame = CGRectMake(5,110, 200, height);
}
//
// MyTableViewCell.m
#import "MyTableViewCell.h"
#import "AjustFrame.h"
@implementationMyTableViewCell
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selectedanimated:(BOOL)animated {
[supersetSelected:selectedanimated:animated];
}
//自定义视图,重新实现初始化方法
-(instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) {
self.imageV =[[UIImageViewalloc]initWithFrame:CGRectMake(5, 5, 120, 150)];
[self.contentViewaddSubview:self.imageV];
self.nameL =[[UILabelalloc]initWithFrame:CGRectMake(130, 5, 200, 30)];
[self.contentViewaddSubview:self.nameL];
self.sexL =[[UILabelalloc]initWithFrame:CGRectMake(130, 40, 200, 30)];
[self.contentViewaddSubview:self.sexL];
self.phoneNumberL =[[UILabelalloc]initWithFrame:CGRectMake(130, 75, 200, 30)];
[self.contentViewaddSubview:self.phoneNumberL];
self.introduceL =[[UILabelalloc]initWithFrame:CGRectMake(130, 110, 250, 60)];
self.introduceL.numberOfLines = 0;
[self.contentViewaddSubview:self.introduceL];
}
return self;
}
//为控件内容赋值
- (void)setStu:(Student *)stu{
self.imageV.image =[UIImageimageNamed:stu.icon];
self.nameL.text = stu.name;
self.phoneNumberL.text = stu.phoneNumber;
self.sexL.text = stu.sex;
self.introduceL.text = stu.introduce;
// // 控件的大小应该在拿到控件的数据之后确定
// // 求出文本高度
// // 求字符串的字体大小
// NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
// // 求出所占高度
// CGFloat height =[self.introduceL.textboundingRectWithSize:CGSizeMake(200, 1000)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size.height;
// // 重新设置label高度
// self.introduceL.frame = CGRectMake(130, 110, 200, height);
// 优化的代码
CGFloat height =[AjustFrameheightForString:self.introduceL.text width:200Font:self.introduceL.font];
self.introduceL.frame = CGRectMake(130,110, 250, height);
}
@end
//声明一个类,专门用来计算字符串的高度,从而使cell能够根据内容的多少来计算高度。
// AjustFrame.m
#import "AjustFrame.h"
@implementationAjustFrame
// 用来计算字符串的高度
+ (CGFloat)heightForString:(NSString *)textwidth:(CGFloat)width Font:(UIFont *)font{
CGFloat height = [textboundingRectWithSize:CGSizeMake(width, 1000)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font}context:nil].size.height;
return height;
}
@end