@interface FYZMalePersonCell : UITableViewCell
@property (nonatomic, retain) PhotoInfo *photoInfo;//接收c传入的M
<span style="background-color: rgb(153, 153, 255);">//动态设置cell的高度
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo;</span>
@end<pre name="code" class="objc">@interface FYZMalePersonCell ()
@property (nonatomic, retain) UIImageView *iconView; //显示图标
@property (nonatomic, retain) UILabel *titleLabel; //显示名字与职称
@property (nonatomic, retain) UILabel *descriptionLabel; //显示图片描述
@property (nonatomic, retain) UIImageView *photoView; //显示图片
@end
@implementation FYZMalePersonCell
//重写setter
- (void)setPhotoInfo:(PhotoInfo *)photoInfo
{
if (_photoInfo != photoInfo) {
[_photoInfo release];
_photoInfo = [photoInfo retain];
}
//为自身控件赋值
//(1)图标
self.iconView.image = [UIImage imageNamed:@"lanou"];
//(2)名字加职称
self.titleLabel.text = [NSString stringWithFormat:@"%@, %@", photoInfo.name, photoInfo.title];
//(3)简介
//修改descriptionLabel的高度
CGRect desFrame = _descriptionLabel.frame;
desFrame.size.height = [[self class] heightForText:photoInfo.introduction];//[self class]获取当前类
_descriptionLabel.frame = desFrame;
self.descriptionLabel.text = photoInfo.introduction;
//(4)图片
//修改photoView高度
CGRect photoFrame = _photoView.frame;
photoFrame.origin.y = desFrame.size.height + desFrame.origin.y;
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
photoFrame.size.height = [[self class] heightForImage:image];
_photoView.frame = photoFrame;
self.photoView.image = image;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//iconView
CGFloat px = kPhotoCell_MarginLeft;
CGFloat py = kPhotoCell_MarginBetween;
CGFloat width = kPhotoCell_IconView_Width;
CGFloat height = kPhotoCell_IconView_Height;
self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
[self.contentView addSubview:_iconView];
[_iconView release];
//titleLabel
px = px + width + kPhotoCell_MarginLeft;
py = py;
width = kPhotoCell_TitleLabel_Width;
height = kPhotoCell_TitleLabel_Height;
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
_titleLabel.font = [UIFont systemFontOfSize:kFont_Size];
_titleLabel.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:_titleLabel];
[_titleLabel release];
//descriptionLabel
px = kPhotoCell_MarginLeft;
py = py + height + kPhotoCell_MarginBetween;
width = kPhotoCell_Width;
height = 0;
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
_descriptionLabel.numberOfLines = 0;
_descriptionLabel.font = [UIFont systemFontOfSize:kFont_Size];
[self.contentView addSubview:_descriptionLabel];
[_descriptionLabel release];
//photoView
px = px;
py = py + height + kPhotoCell_MarginBetween;
width = kPhotoCell_Width;
height = 0;
self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
[self.contentView addSubview:_photoView];
[_photoView release];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
<span style="background-color: rgb(153, 153, 255);">+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
//1.图片高度</span>
//让图片等比例缩放(防止图片失真)
//(1)获取图片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
CGFloat imageHeight = [self heightForImage:image];
//2.文本的高度
CGFloat textHeight = [self heightForText:photoInfo.introduction];
//3.返回cell的总高度
return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 3 * kPhotoCell_MarginBetween;
}
//单独计算图片的高度
<span style="background-color: rgb(153, 153, 255);">+ (CGFloat)heightForImage:(UIImage *)image
{</span>
//(2)获取图片的大小
CGSize size = image.size;
//(3)求出缩放比例
CGFloat scale = kPhotoCell_Width / size.width;
CGFloat imageHeight = size.height * scale;
return imageHeight;
}
<span style="background-color: rgb(153, 153, 255);">//单独计算文本的高度
+ (CGFloat)heightForText:(NSString *)text
{</span>
//2.文本的高度
//设置计算文本时,字体的大小,以什么标准来计算
//第一个参数:限制范围 2个:设置文本高和间距 3个:字体的大小
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kFont_Size]};
CGSize textSize = [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
return textSize.height;
}
- (void)dealloc
{
RELEASE_SAFE(_descriptionLabel);
RELEASE_SAFE(_iconView);
RELEASE_SAFE(_titleLabel);
RELEASE_SAFE(_photoView);
RELEASE_SAFE(_photoInfo);
[super dealloc];
}
@end
female cell
@interface FYZFemalePersonCell : UITableViewCell
@property (nonatomic, retain) PhotoInfo *photoInfo;
<span style="background-color: rgb(153, 153, 255);">//动态设置cell的高度
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo;
@end</span>
@interface FYZFemalePersonCell ()
@property (nonatomic, retain) UIImageView *iconView; //显示图标
@property (nonatomic, retain) UILabel *titleLabel; //显示名字与职称
@property (nonatomic, retain) UILabel *descriptionLabel; //显示图片描述
@property (nonatomic, retain) UIImageView *photoView; //显示图片
@end
@implementation FYZFemalePersonCell
//重写setter方法为控件赋值
- (void)setPhotoInfo:(PhotoInfo *)photoInfo
{
if (_photoInfo != photoInfo) {
[_photoInfo release];
_photoInfo = [photoInfo retain];
}
//赋值
//(1)图标
self.iconView.image = [UIImage imageNamed:@"lanou"];
//(2)titleLabel
self.titleLabel.text = [NSString stringWithFormat:@"%@, %@", photoInfo.name, photoInfo.title];
//(3)图片
//修改photoView高度
CGRect photoFrame = _photoView.frame;
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
photoFrame.size.height = [[self class] heightForImage:image];
//_photoView.frame = photoFrame;
self.photoView.image = image;
//(4)简介
//修改descriptionLabel的高度
CGRect desFrame = _descriptionLabel.frame;
desFrame.size.height = [[self class] heightForText:photoInfo.introduction];//[self class]获取当前类
desFrame.origin.y = photoFrame.size.height + photoFrame.origin.y;
_descriptionLabel.frame = desFrame;
self.descriptionLabel.text = photoInfo.introduction;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//titleLabel
CGFloat px = kPhotoCell_MarginLeft;
CGFloat py = kPhotoCell_MarginBetween;
CGFloat width = kPhotoCell_TitleLabel_Width;
CGFloat height = kPhotoCell_TitleLabel_Height;
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
[self.contentView addSubview:_titleLabel];
[_titleLabel release];
//iconView
px = px + width + kPhotoCell_MarginLeft;
py = py;
width = kPhotoCell_IconView_Width;
height = kPhotoCell_IconView_Height;
self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
[self.contentView addSubview:_iconView];
[_iconView release];
//descriptionLabel
px = kPhotoCell_MarginLeft;
py = py + height + kPhotoCell_MarginBetween;
width = kPhotoCell_Width;
height = 0;
self.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(px, py, width, height)];
[self.contentView addSubview:_photoView];
[_photoView release];
//photoView
px = px;
py = py + height;
width = kPhotoCell_Width;
height = 0;
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(px, py, width, height)];
_descriptionLabel.font = [UIFont systemFontOfSize:kFont_Size];
_descriptionLabel.numberOfLines = 0;
[self.contentView addSubview:_descriptionLabel];
[_descriptionLabel release];
}
return self;
}
+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
//1.图片高度
//让图片等比例缩放(防止图片失真)
//(1)获取图片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FYZ" ofType:@"png"]];
CGFloat imageHeight = [self heightForImage:image];
//2.文本的高度
CGFloat textHeight = [self heightForText:photoInfo.introduction];
//3.返回cell的总高度
return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 3 * kPhotoCell_MarginBetween;
}
//单独计算图片的高度
+ (CGFloat)heightForImage:(UIImage *)image
{
//(2)获取图片的大小
CGSize size = image.size;
//(3)求出缩放比例
CGFloat scale = kPhotoCell_Width / size.width;
CGFloat imageHeight = size.height * scale;
return imageHeight;
}
//单独计算文本的高度
+ (CGFloat)heightForText:(NSString *)text
{
//2.文本的高度
//设置计算文本时,字体的大小,以什么标准来计算
//第一个参数:限制范围 2个:设置文本高和间距 3个:字体的大小
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:kFont_Size]};
CGSize textSize = [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
return textSize.height;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
RELEASE_SAFE(_descriptionLabel);
RELEASE_SAFE(_iconView);
RELEASE_SAFE(_titleLabel);
RELEASE_SAFE(_photoView);
[super dealloc];
}
@end
@interface <span style="background-color: rgb(153, 153, 255);">FYZPhotoPersonHelper</span> : NSObject
+ (FYZPhotoPersonHelper *)sharedPhotoPersonHelper;
//分区数
+ (NSInteger)numberOfSections;
//分区元素
+ (NSInteger) numberOfRowsInSection:(NSInteger)section;
//返回photoInfo对象
+ (PhotoInfo *)photoInfoAtIndexPath:(NSIndexPath *)indexPath;
@end
@interface FYZPhotoPersonHelper ()
@property (nonatomic, retain) NSMutableArray *photoArr;
@end
@implementation FYZPhotoPersonHelper
static FYZPhotoPersonHelper *helper = nil;
+ (FYZPhotoPersonHelper *)sharedPhotoPersonHelper
{
@synchronized(self) {
if (!helper) {
helper = [[FYZPhotoPersonHelper alloc] init];
[helper readDataFromPlist];
}
}
return helper;
}
- (void<span style="background-color: rgb(153, 153, 255);">)readDataFromPlist</span>
{
//获取路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PresonList" ofType:@"plist"];
//根据路径初始化数组对象
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
//创建可变数组,存储封装好的photoInfo对象
self.photoArr = [NSMutableArray array];
//遍历数组,将小子点封装成photoInfo对象
for (NSDictionary *tempDic in arr) {
PhotoInfo *photo = [[PhotoInfo alloc] initWithDic:tempDic];
[_photoArr addObject:photo];
RELEASE_SAFE(photo);
}
}
+ (NSInteger)numberOfSections
{
//先调用这个方法创建helper
[FYZPhotoPersonHelper sharedPhotoPersonHelper];
return 1;
}
+ (NSInteger) numberOfRowsInSection:(NSInteger)section
{
return helper.photoArr.count;
}
+ (PhotoInfo *)photoInfoAtIndexPath:(NSIndexPath *)indexPath
{
return helper.photoArr[indexPath.row];
}
@end