Controller:
1 // 2 // ViewController.m 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/4. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Weibo.h" 11 #import "WeiboCell.h" 12 #import "WeiboFrame.h" 13 14 @interface ViewController () 15 16 /** 微博数组,类型是WeiboFrame,包含了数据和位置尺寸信息 */ 17 @property(nonatomic, strong) NSArray *weibos; 18 19 @end 20 21 @implementation ViewController 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 // Do any additional setup after loading the view, typically from a nib. 26 } 27 28 - (void)didReceiveMemoryWarning { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 // 屏蔽状态栏 34 - (BOOL)prefersStatusBarHidden { 35 return YES; 36 } 37 38 #pragma mark - 数据源操作 39 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 40 return self.weibos.count; 41 } 42 43 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 44 // 传入tableView是为了使用cell缓存池 45 WeiboCell *cell = [WeiboCell cellWithTableView:self.tableView]; 46 47 // 传入微博的数据和位置尺寸信息 48 cell.weiboFrame = self.weibos[indexPath.row]; 49 50 return cell; 51 } 52 53 54 #pragma mark - 加载数据 55 // 延迟加载plist文件中的数据为微博数组 56 - (NSArray *) weibos { 57 if (nil == _weibos) { 58 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]]; 59 60 NSMutableArray *mdictArray = [NSMutableArray array]; 61 for (NSDictionary *dict in dictArray) { 62 WeiboFrame *weiboFrame = [[WeiboFrame alloc] init]; 63 Weibo *weibo = [Weibo weiboWithDictionary:dict]; 64 65 // 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸 66 weiboFrame.weibo = weibo; 67 68 [mdictArray addObject:weiboFrame]; 69 } 70 71 _weibos = mdictArray; 72 } 73 74 return _weibos; 75 } 76 77 78 #pragma mark - 代理操作 79 // 动态调整每个cell的高度 80 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 81 WeiboFrame *weiboFrame = self.weibos[indexPath.row]; 82 return weiboFrame.cellHeight; 83 } 84 85 @end
View:
1 // 2 // WeiboCell.h 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 // 用于装载每个TabelViewCell的model 10 #import <UIKit/UIKit.h> 11 12 @class WeiboFrame; 13 @interface WeiboCell : UITableViewCell 14 15 // 微博frame,内持有微博数据和尺寸、位置信息 16 @property(nonatomic, strong) WeiboFrame *weiboFrame; 17 18 19 // 自定义带有父控件tableView初始化方法 20 + (instancetype) cellWithTableView:(UITableView *) tableView; 21 22 @end
1 // 2 // WeiboCell.m 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "WeiboCell.h" 10 #import "WeiboFrame.h" 11 #import "Weibo.h" 12 13 // 昵称字体 14 #define NAME_FONT [UIFont systemFontOfSize:14] 15 // 博文字体 16 #define TEXT_FONT [UIFont systemFontOfSize:15] 17 18 19 @interface WeiboCell() 20 21 // 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整 22 /** 头像 */ 23 @property(nonatomic, weak) UIImageView *iconView; 24 25 /** 昵称 */ 26 @property(nonatomic, weak) UILabel *nameView; 27 28 /** vip标志 */ 29 @property(nonatomic, weak) UIImageView *vipView; 30 31 /** 博文 */ 32 @property(nonatomic, weak) UILabel *textView; 33 34 /** 配图 */ 35 @property(nonatomic, weak) UIImageView *pictureView; 36 37 @end 38 39 @implementation WeiboCell 40 41 - (void)awakeFromNib { 42 // Initialization code 43 } 44 45 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 46 [super setSelected:selected animated:animated]; 47 48 // Configure the view for the selected state 49 } 50 51 #pragma mark - 初始化 52 // 自定义带有父控件tableView初始化方法 53 + (instancetype) cellWithTableView:(UITableView *) tableView { 54 static NSString *ID = @"weibo"; 55 56 // 从缓存池寻找 57 WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 58 59 // 使用重写的构造方法初始化 60 if (nil == cell) { 61 cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 62 } 63 64 return cell; 65 } 66 67 // 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息 68 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 69 if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 70 // 1.头像 71 /** 72 由于self.iconView是weak类型,不能写成: 73 self.iconView = [[UIImageView alloc] init]; 74 会被立即释放,不能正常赋值,下同 75 */ 76 UIImageView *iconView = [[UIImageView alloc] init]; 77 [self.contentView addSubview:iconView]; 78 self.iconView = iconView; 79 80 // 2.昵称 81 UILabel *nameView = [[UILabel alloc] init]; 82 // 指定字体用来计算占用的尺寸大小 83 nameView.font = NAME_FONT; 84 [self.contentView addSubview:nameView]; 85 self.nameView = nameView; 86 87 // 3.vip标志 88 UIImageView *vipView = [[UIImageView alloc] init]; 89 vipView.image = [UIImage imageNamed:@"vip"]; 90 [self.contentView addSubview:vipView]; 91 self.vipView = vipView; 92 93 // 4.博文 94 UILabel *textView = [[UILabel alloc] init]; 95 textView.font = TEXT_FONT; 96 textView.numberOfLines = 0;// 设置自动换行 97 [self.contentView addSubview:textView]; 98 self.textView = textView; 99 100 // 5.配图 101 UIImageView *pictureView = [[UIImageView alloc] init]; 102 [self.contentView addSubview:pictureView]; 103 self.pictureView = pictureView; 104 } 105 106 return self; 107 } 108 109 #pragma mark - 数据加载 110 // 加载数据的时候设置数据和尺寸、位置 111 - (void)setWeiboFrame:(WeiboFrame *)weiboFrame { 112 _weiboFrame = weiboFrame; 113 114 // 1.设置数据 115 [self calWeiboData]; 116 117 // 2.设置尺寸、位置 118 [self calWeiboFrame]; 119 } 120 121 // 设置数据 122 - (void) calWeiboData { 123 Weibo *weibo = self.weiboFrame.weibo; 124 125 // 1.头像 126 self.iconView.image = [UIImage imageNamed:weibo.icon]; 127 128 // 2.昵称 129 self.nameView.text = weibo.name; 130 131 // 3.vip标志 132 if (weibo.vip) { 133 self.vipView.hidden = NO; 134 } 135 else { 136 self.vipView.hidden = YES; 137 } 138 139 // 4.博文 140 self.textView.text = weibo.text; 141 142 143 // 5.配图 144 if (weibo.picture) { 145 self.pictureView.hidden = NO; 146 self.pictureView.image = [UIImage imageNamed:weibo.picture]; 147 } 148 else { 149 self.pictureView.hidden = YES; 150 self.pictureView.image = nil; 151 } 152 } 153 154 // 设置位置、尺寸 155 - (void) calWeiboFrame { 156 // 1.头像 157 self.iconView.frame = self.weiboFrame.iconFrame; 158 159 // 2.昵称 160 self.nameView.frame = self.weiboFrame.nameFrame; 161 162 // 3.vip标志 163 self.vipView.frame = self.weiboFrame.vipFrame; 164 165 // 4.博文 166 self.textView.frame = self.weiboFrame.textFrame; 167 168 // 5.配图 169 if (self.weiboFrame.weibo.picture) { 170 self.pictureView.frame = self.weiboFrame.pictureFrame; 171 } 172 } 173 174 175 @end
Model:
1 // 2 // Weibo.h 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 // 装在微博数据的model 10 #import <Foundation/Foundation.h> 11 12 @interface Weibo : NSObject 13 14 #pragma mark - 成员变量 15 /** 头像 */ 16 @property(nonatomic, copy) NSString *icon; 17 18 /** 昵称 */ 19 @property(nonatomic, copy) NSString *name; 20 21 /** vip标志 */ 22 @property(nonatomic, assign) BOOL vip; 23 24 /** 博文 */ 25 @property(nonatomic, copy) NSString *text; 26 27 /** 配图 */ 28 @property(nonatomic, copy) NSString *picture; 29 30 31 #pragma mark - 自定义初始化方法 32 /** 使用字典赋值成员 */ 33 - (instancetype) initWithDictionary:(NSDictionary *) dictionary; 34 35 /** 使用字典赋值成员 */ 36 + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary; 37 38 /** 返回空的model */ 39 + (instancetype) weibo; 40 41 @end
1 // 2 // Weibo.m 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "Weibo.h" 10 11 @implementation Weibo 12 13 /** 使用字典赋值成员 */ 14 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 15 if (self = [super init]) { 16 [self setValuesForKeysWithDictionary:dictionary]; 17 } 18 19 return self; 20 } 21 22 /** 使用字典赋值成员 */ 23 + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary { 24 return [[self alloc] initWithDictionary:dictionary]; 25 } 26 27 /** 返回空的model */ 28 + (instancetype) weibo { 29 return [self weiboWithDictionary:nil]; 30 } 31 32 @end
1 // 2 // WeiboFrame.h 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 // 装在了每个cell的位置、尺寸和微博数据的model 10 11 @class Weibo; 12 #import <Foundation/Foundation.h> 13 #import <UIKit/UIKit.h> // CGRect需要引入UIKit 14 15 @interface WeiboFrame : NSObject 16 17 // 微博数据 18 @property(nonatomic, strong) Weibo *weibo; 19 20 /** 头像 */ 21 @property(nonatomic, assign, readonly) CGRect iconFrame; 22 23 /** 昵称 */ 24 @property(nonatomic, assign, readonly) CGRect nameFrame; 25 26 /** vip标志 */ 27 @property(nonatomic, assign, readonly) CGRect vipFrame; 28 29 /** 博文 */ 30 @property(nonatomic, assign, readonly) CGRect textFrame; 31 32 /** 配图 */ 33 @property(nonatomic, assign, readonly) CGRect pictureFrame; 34 35 /** 一条微博cell的高度 */ 36 @property(nonatomic, assign, readonly) CGFloat cellHeight; 37 38 39 @end
1 // 2 // WeiboFrame.m 3 // Weibo 4 // 5 // Created by hellovoidworld on 14/12/5. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "WeiboFrame.h" 10 #import "Weibo.h" 11 12 // 昵称字体 13 #define NAME_FONT [UIFont systemFontOfSize:14] 14 // 博文字体 15 #define TEXT_FONT [UIFont systemFontOfSize:15] 16 17 @implementation WeiboFrame 18 19 #pragma mark - 加载数据 20 // 加载数据,用以计算各个控件的位置、尺寸 21 - (void)setWeibo:(Weibo *)weibo { 22 _weibo = weibo; 23 24 // 间隙参数 25 CGFloat padding = 10; 26 27 // 1.头像 28 CGFloat iconWidth = 30; 29 CGFloat iconHeight = 30; 30 CGFloat iconX = padding; 31 CGFloat iconY = padding; 32 _iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight); 33 34 // 2.昵称 35 // 计算昵称占用的size 36 CGSize nameSize = [self calTextSizeWithText:self.weibo.name font:TEXT_FONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)]; 37 38 CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding; 39 CGFloat nameY = iconY + (iconHeight - nameSize.height) / 2;// 居中 40 _nameFrame.size = nameSize; 41 _nameFrame.origin = CGPointMake(nameX, nameY); 42 43 // 3.vip标志 44 CGFloat vipWith = 14; 45 CGFloat vipHeight = 14; 46 CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding; 47 CGFloat vipY = nameY; 48 _vipFrame = CGRectMake(vipX, vipY, vipWith, vipHeight); 49 50 // 4.博文 51 CGSize textSize = [self calTextSizeWithText:self.weibo.text font:TEXT_FONT maxSize:CGSizeMake(300, MAXFLOAT)]; 52 CGFloat textX = padding; 53 CGFloat textY = CGRectGetMaxY(_iconFrame) + padding; 54 _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height); 55 56 // 5.配图 57 if (self.weibo.picture) { 58 CGFloat pictureWidth = 100; 59 CGFloat pictureHeight = 100; 60 CGFloat pictureX = padding; 61 CGFloat pictureY = CGRectGetMaxY(_textFrame) + padding; 62 _pictureFrame = CGRectMake(pictureX, pictureY, pictureWidth, pictureHeight); 63 64 _cellHeight = CGRectGetMaxY(_pictureFrame) + padding; //计算cell高度 65 } 66 else { 67 _cellHeight = CGRectGetMaxY(_textFrame) + padding; 68 } 69 } 70 71 // 使用自带方法计算一段文字占用的size 72 - (CGSize) calTextSizeWithText:(NSString *) text font:(UIFont *) font maxSize:(CGSize) maxSize { 73 NSDictionary *attrs = @{NSFontAttributeName : font}; 74 75 return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; 76 } 77 78 @end 79
weibo.plist:
images: