标签:style io ar color 使用 sp for strong on
///-------1.1数据模型.h---------
#import <Foundation/Foundation.h> @interface MLStatus : NSObject @property(nonatomic, copy) NSString *text; @property(nonatomic, copy) NSString *icon; @property(nonatomic, copy) NSString *name; @property(nonatomic, copy) NSString *picture; @property(nonatomic, assign) BOOL vip; +(instancetype)statusWithDict:(NSDictionary *)dict; -(instancetype)initWithDict:(NSDictionary *)dict; @end
///-------1.2数据模型.m---------
#import "MLStatus.h" @implementation MLStatus +(instancetype)statusWithDict:(NSDictionary *)dict{ return [[MLStatus alloc]initWithDict:dict]; } -(instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { //使用kvc(BOOL 和 int 类型kvc也可以实现转化) [self setValuesForKeysWithDictionary:dict]; } return self; } @end
///-------2.1cell的frame模型.h---------
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h>//如果是用XCode6的话,".h"文件中无法使用CGRect和CGFloat,需要使用改头文件 @class MLStatus; @interface MLStatusFrame : NSObject //头像的frame @property(nonatomic, assign, readonly) CGRect iconF; //昵称的frame @property(nonatomic, assign, readonly) CGRect nameF; //会员图标的frame @property(nonatomic, assign, readonly) CGRect vipF; //正文的frame @property(nonatomic, assign, readonly) CGRect textF; //配图的frame @property(nonatomic, assign, readonly) CGRect pictureF; //cell的高度 @property(nonatomic, assign, readonly) CGFloat cellHeight; @property(nonatomic, strong) MLStatus *status; @end
///-------2.2cell的frame模型.m---------
#import "MLStatusFrame.h" #import "MLStatus.h" //昵称的字体 #define MLNameFont [UIFont systemFontOfSize:14] //正文的字体 #define MLTextFont [UIFont systemFontOfSize:15] @implementation MLStatusFrame //重写set方法 -(void)setStatus:(MLStatus *)status{ _status = status; //子控件之间的间距 CGFloat padding = 10; //头像 CGFloat iconX = padding; CGFloat iconY = padding; CGFloat iconW = 30; CGFloat iconH = 30; //成员是readonly属性,也就相当于没有setter方法,不能用.语法方法,只能通过_方式来访问 _iconF= CGRectMake(iconX, iconY, iconW, iconH); //昵称 CGSize nameSize = [self sizeWithText:self.status.name font:MLNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)]; CGFloat nameX = CGRectGetMaxX(_iconF) +padding; CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5; _nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height); //会员图标 CGFloat vipX = CGRectGetMaxX(_nameF) + padding; CGFloat vipY = nameY; CGFloat vipW = 14; CGFloat vipH = 14; _vipF = CGRectMake(vipX, vipY, vipW, vipH); //正文 CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY(_iconF) + padding; CGSize textSize = [self sizeWithText:self.status.text font:MLTextFont maxSize:CGSizeMake(300, MAXFLOAT)]; _textF = CGRectMake(textX ,textY, textSize.width, textSize.height); //配图 if (self.status.picture) { CGFloat pictureX = textX; CGFloat pictureY = CGRectGetMaxY(_textF) + padding; CGFloat pictureW = 100; CGFloat pictureH = 100; _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH); _cellHeight = CGRectGetMaxY(_pictureF) + padding; }else{ _cellHeight = CGRectGetMaxY(_textF) + padding; } } -(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingRectWithSize: maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } @end
///-------3.1自定义cell.h---------
#import <UIKit/UIKit.h> @class MLStatusFrame; @interface MLStatusCell : UITableViewCell @property(nonatomic, strong) MLStatusFrame *statusFrame; + (instancetype)cellWithTableView:(UITableView *)tableView; @end
///-------3.2自定义cell.m---------
#import "MLStatusCell.h" #import "MLStatus.h" #import "MLStatusFrame.h" //昵称的字体 #define MLNameFont [UIFont systemFontOfSize:14] //正文的字体 #define MLTextFont [UIFont systemFontOfSize:15] @interface MLStatusCell() @property(nonatomic, weak) UIImageView *iconView; @property(nonatomic, weak) UILabel *nameView; @property(nonatomic, weak) UIImageView *vipView; @property(nonatomic, weak) UILabel *textView; @property(nonatomic, weak) UIImageView *pictureView; @end @implementation MLStatusCell -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //自定义cell,一定要把子控件添加到contentView中 //只添所有加子控件(不设置数据和frame) //头像 UIImageView *iconView = [[UIImageView alloc]init]; [self.contentView addSubview:iconView]; self.iconView = iconView; //昵称 UILabel *nameView = [[UILabel alloc]init]; nameView.font = MLNameFont; [self.contentView addSubview:nameView]; self.nameView = nameView; //会员图标 UIImageView *vipView = [[UIImageView alloc]init]; vipView.image = [UIImage imageNamed:@"vip"]; [self.contentView addSubview:vipView]; self.vipView = vipView; //正文 UILabel *textView = [[UILabel alloc]init]; textView.font = MLTextFont; textView.numberOfLines = 0; [self.contentView addSubview:textView]; self.textView = textView; //配图 UIImageView *pictureView = [[UIImageView alloc]init]; [self.contentView addSubview:pictureView]; self.pictureView = pictureView; } return self; } //在这个方法中设置子控件的frame和显示数据. -(void) setStatusFrame:(MLStatusFrame *)statusFrame{ _statusFrame = statusFrame; //给子控件设置数据 [self settingData]; //给子控件设置frame [self settingFrame]; } //设置数据 -(void)settingData{ //微博数据 MLStatus *status = self.statusFrame.status; //头像 self.iconView.image = [UIImage imageNamed:status.icon]; //昵称 self.nameView.text = status.name; //会员 if (status.vip) { self.vipView.hidden = NO; self.nameView.textColor = [UIColor redColor]; }else{ self.vipView.hidden = YES; self.nameView.textColor = [UIColor blackColor]; } //正文 self.textView.text = status.text; //配图 if(status.picture){ self.pictureView.hidden = NO; self.pictureView.image = [UIImage imageNamed:status.picture]; }else{ self.pictureView.hidden = YES; } } -(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingRectWithSize: maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } //设置frame -(void)settingFrame{ //头像 self.iconView.frame = self.statusFrame.iconF; //昵称 self.nameView.frame = self.statusFrame.nameF; //会员图标 self.vipView.frame = self.statusFrame.vipF; //正文 self.textView.frame = self.statusFrame.textF; //配图 if(self.statusFrame.status.picture){ self.pictureView.frame = self.statusFrame.pictureF; } } + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"status"; MLStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[MLStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell; } @end
///-------4.1自定义控制器.h---------
#import <UIKit/UIKit.h> @interface MLViewController : UITableViewController @end
///-------4.2自定义控制器.m---------
#import "MLViewController.h" #import "MLStatus.h" #import "MLStatusCell.h" #import "MLStatusFrame.h" @interface MLViewController () @property(nonatomic, strong)NSArray *statusFrames; @end @implementation MLViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.showsVerticalScrollIndicator = NO; } -(NSArray *)statusFrames{ if (_statusFrames == nil) { //获得plist的全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]; //加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; //将dictArray里面的所有字典转成模型,放到新的数组里 NSMutableArray *statusFrames = [NSMutableArray array]; for(NSDictionary *dict in dictArray){ //创建MLStatus模型 MLStatus *status = [MLStatus statusWithDict:dict]; //创建MLStatusFrame模型 MLStatusFrame *statusFrame = [[MLStatusFrame alloc]init]; statusFrame.status = status; [statusFrames addObject:statusFrame]; } _statusFrames = statusFrames; } return _statusFrames; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.statusFrames.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //创建cell MLStatusCell *cell = [MLStatusCell cellWithTableView:tableView]; //设置高度 cell.statusFrame = self.statusFrames[indexPath.row]; //返回cell return cell; } -(BOOL)prefersStatusBarHidden{ return YES; } #pragma mark - 实现代理方法 //自定义没个cell的高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // 取出这行对应的frame模型 MLStatusFrame *statusFrame = self.statusFrames[indexPath.row]; return statusFrame.cellHeight; } @end
标签:style io ar color 使用 sp for strong on
原文地址:http://my.oschina.net/u/2285956/blog/355637