// // ViewController.m // 04-自适应cell // // Created by ?? on 15/5/22. // Copyright (c) 2015年 sczy. All rights reserved. // #import "ViewController.h" #import "JWStatus.h" #import "JWTableViewCell.h" #import "JWStatusFrame.h" @interfaceViewController () @property (strong, nonatomic) NSArray *statusesFrameDatas; @end @implementation ViewController -(NSArray *)statusesFrameDatas { if (_statusesFrameDatas == nil) { NSArray *allDatas = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"statuses.plist"ofType:nil]]; NSMutableArray *tempArr = [NSMutableArrayarray]; for (NSDictionary *dict in allDatas) { JWStatus *status = [JWStatusstatusWithDict:dict]; JWStatusFrame *statusF = [[JWStatusFramealloc]init]; statusF.status = status; [tempArr addObject:statusF]; } _statusesFrameDatas = tempArr; } return_statusesFrameDatas; } - (void)viewDidLoad { [superviewDidLoad]; self.tableView.rowHeight = 300; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { returnself.statusesFrameDatas.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); JWTableViewCell *cell = [JWTableViewCellcellWithTableView:tableView]; JWStatusFrame *statusF = self.statusesFrameDatas[indexPath.row]; cell.statusF = statusF; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 1. frame模型保存cell的高度的数据 JWStatusFrame *statusF = self.statusesFrameDatas[indexPath.row]; return statusF.cellHeight; } @end |
#import "JWStatusFrame.h" #import "JWStatus.h" #define kNameLabelFont [UIFont systemFontOfSize:15] #define kIntroLabelFont [UIFont systemFontOfSize:14] @implementation JWStatusFrame -(void)setStatus:(JWStatus *)status { _status = status; // 计算子控件的frame和cell的高度 CGFloat margin = 10; CGFloat iconViewX = margin; CGFloat iconViewY = margin; CGFloat iconViewW = 35; CGFloat iconViewH = iconViewW; _iconViewF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH); // 计算宽高()sizeWithFont: iOS6用这个 iOS之后用下面这个 // 指定我要以这个大小的字体显示 NSDictionary *attributeDict = @{NSFontAttributeName : kNameLabelFont}; // 最大范围 CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT); // 就是Name的size CGSize size = [status.nameboundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOriginattributes:attributeDict context:nil].size; CGFloat nameLabelX = margin + CGRectGetMaxX(_iconViewF); CGFloat nameLabelW = size.width; CGFloat nameLabelH = size.height; CGFloat nameLabelY = _iconViewF.origin.y + (_iconViewF.size.height - nameLabelH) * 0.5; _nameLabelF = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH); // vip if (status.vip) { CGFloat vipViewX = margin + CGRectGetMaxX(_nameLabelF); CGFloat vipViewW = 14; CGFloat vipViewH = vipViewW; CGFloat vipViewY = _nameLabelF.origin.y; _vipViewF = CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH); } // intro CGSize maxSize2 = CGSizeMake(375 - margin * 2, MAXFLOAT); NSDictionary *attributeDict2 = @{NSFontAttributeName : kIntroLabelFont}; CGSize introLabelSize = [status.textboundingRectWithSize:maxSize2 options:NSStringDrawingUsesLineFragmentOriginattributes:attributeDict2 context:nil].size; CGFloat introLabelX = margin; CGFloat introLabelW = introLabelSize.width; CGFloat introLabelH = introLabelSize.height; CGFloat introLabelY = CGRectGetMaxY(_iconViewF) + margin; _introLabelF = CGRectMake(introLabelX, introLabelY, introLabelW, introLabelH); // picture if (status.picture) { CGFloat pictureViewX = margin; CGFloat pictureViewW = 150; CGFloat pictureViewH = pictureViewW; CGFloat pictureViewY = CGRectGetMaxY(_introLabelF) + margin; _pictureViewF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH); _cellHeight = CGRectGetMaxY(_pictureViewF) + margin; } else { _cellHeight = CGRectGetMaxY(_introLabelF) + margin; } } @end |
#import "JWTableViewCell.h" #import "JWStatus.h" #import "JWStatusFrame.h" #define kNameLabelFont [UIFont systemFontOfSize:15] #define kIntroLabelFont [UIFont systemFontOfSize:14] @interfaceJWTableViewCell() @property (strong, nonatomic) UIImageView *iconView; @property (strong, nonatomic) UILabel *nameLabel; @property (strong, nonatomic) UIImageView *vipView; @property (strong, nonatomic) UILabel *introLabel; @property (strong, nonatomic) UIImageView *pictureView; @end @implementation JWTableViewCell +(instancetype)cellWithTableView:(UITableView *)tableView { staticNSString *str = @"cell"; JWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (!cell) { cell = [[JWTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]; } return cell; } -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) { // 添加自己可能显示的所有子控件 UIImageView *iconView = [[UIImageViewalloc]init]; [self.contentViewaddSubview:iconView]; self.iconView = iconView; UILabel *nameLabel = [[UILabelalloc]init]; nameLabel.font = kNameLabelFont; nameLabel.numberOfLines = 0; [self.contentViewaddSubview:nameLabel]; self.nameLabel = nameLabel; UIImageView *vipView = [[UIImageViewalloc]init]; vipView.image = [UIImageimageNamed:@"vip"]; [self.contentViewaddSubview:vipView]; self.vipView = vipView; UILabel *introLabel = [[UILabelalloc]init]; introLabel.font = kIntroLabelFont; introLabel.numberOfLines = 0; [self.contentViewaddSubview:introLabel]; self.introLabel = introLabel; UIImageView *pictureView = [[UIImageViewalloc]init]; [self.contentViewaddSubview:pictureView]; self.pictureView = pictureView; } returnself; } -(void)setStatusF:(JWStatusFrame *)statusF { _statusF = statusF; // 给子控件赋值 [selfsetSubViewsData:statusF]; // 设置子控件的frame [selfsetSubViewsFrame:statusF]; } -(void)setSubViewsData:(JWStatusFrame *)statusF { JWStatus *status = statusF.status; self.iconView.image = [UIImageimageNamed:status.icon]; self.nameLabel.text = status.name; if (statusF.status.vip) { self.nameLabel.textColor = [UIColorredColor]; } else { self.nameLabel.textColor = [UIColorblackColor]; } self.introLabel.text = status.text; self.pictureView.image = [UIImageimageNamed:status.picture]; } -(void)setSubViewsFrame:(JWStatusFrame *)statusF { // 计算子控件的frame self.iconView.frame = statusF.iconViewF; self.nameLabel.frame = statusF.nameLabelF; self.vipView.frame = statusF.vipViewF; self.introLabel.frame = statusF.introLabelF; self.pictureView.frame = statusF.pictureViewF; } @end |