///-------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---------atom
#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---------spa
#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---------code
#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---------orm
#import <UIKit/UIKit.h> @interface MLViewController : UITableViewController @end
///-------4.2自定義控制器.m---------ip
#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