Controller:
 //
// ViewController.m
// Weibo
//
// Created by hellovoidworld on 14/12/4.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "Weibo.h"
#import "WeiboCell.h"
#import "WeiboFrame.h" @interface ViewController () /** 微博数组,类型是WeiboFrame,包含了数据和位置尺寸信息 */
@property(nonatomic, strong) NSArray *weibos; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} // 屏蔽状态栏
- (BOOL)prefersStatusBarHidden {
return YES;
} #pragma mark - 数据源操作
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.weibos.count;
} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 传入tableView是为了使用cell缓存池
WeiboCell *cell = [WeiboCell cellWithTableView:self.tableView]; // 传入微博的数据和位置尺寸信息
cell.weiboFrame = self.weibos[indexPath.row]; return cell;
} #pragma mark - 加载数据
// 延迟加载plist文件中的数据为微博数组
- (NSArray *) weibos {
if (nil == _weibos) {
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]]; NSMutableArray *mdictArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
WeiboFrame *weiboFrame = [[WeiboFrame alloc] init];
Weibo *weibo = [Weibo weiboWithDictionary:dict]; // 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸
weiboFrame.weibo = weibo; [mdictArray addObject:weiboFrame];
} _weibos = mdictArray;
} return _weibos;
} #pragma mark - 代理操作
// 动态调整每个cell的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
WeiboFrame *weiboFrame = self.weibos[indexPath.row];
return weiboFrame.cellHeight;
} @end
 
View:
 //
// WeiboCell.h
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// // 用于装载每个TabelViewCell的model
#import <UIKit/UIKit.h> @class WeiboFrame;
@interface WeiboCell : UITableViewCell // 微博frame,内持有微博数据和尺寸、位置信息
@property(nonatomic, strong) WeiboFrame *weiboFrame; // 自定义带有父控件tableView初始化方法
+ (instancetype) cellWithTableView:(UITableView *) tableView; @end
 
 //
// WeiboCell.m
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "WeiboCell.h"
#import "WeiboFrame.h"
#import "Weibo.h" // 昵称字体
#define NAME_FONT [UIFont systemFontOfSize:14]
// 博文字体
#define TEXT_FONT [UIFont systemFontOfSize:15] @interface WeiboCell() // 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整
/** 头像 */
@property(nonatomic, weak) UIImageView *iconView; /** 昵称 */
@property(nonatomic, weak) UILabel *nameView; /** vip标志 */
@property(nonatomic, weak) UIImageView *vipView; /** 博文 */
@property(nonatomic, weak) UILabel *textView; /** 配图 */
@property(nonatomic, weak) UIImageView *pictureView; @end @implementation WeiboCell - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} #pragma mark - 初始化
// 自定义带有父控件tableView初始化方法
+ (instancetype) cellWithTableView:(UITableView *) tableView {
static NSString *ID = @"weibo"; // 从缓存池寻找
WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 使用重写的构造方法初始化
if (nil == cell) {
cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} return cell;
} // 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 1.头像
/**
由于self.iconView是weak类型,不能写成:
self.iconView = [[UIImageView alloc] init];
会被立即释放,不能正常赋值,下同
*/
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView; // 2.昵称
UILabel *nameView = [[UILabel alloc] init];
// 指定字体用来计算占用的尺寸大小
nameView.font = NAME_FONT;
[self.contentView addSubview:nameView];
self.nameView = nameView; // 3.vip标志
UIImageView *vipView = [[UIImageView alloc] init];
vipView.image = [UIImage imageNamed:@"vip"];
[self.contentView addSubview:vipView];
self.vipView = vipView; // 4.博文
UILabel *textView = [[UILabel alloc] init];
textView.font = TEXT_FONT;
textView.numberOfLines = ;// 设置自动换行
[self.contentView addSubview:textView];
self.textView = textView; // 5.配图
UIImageView *pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureView];
self.pictureView = pictureView;
} return self;
} #pragma mark - 数据加载
// 加载数据的时候设置数据和尺寸、位置
- (void)setWeiboFrame:(WeiboFrame *)weiboFrame {
_weiboFrame = weiboFrame; // 1.设置数据
[self calWeiboData]; // 2.设置尺寸、位置
[self calWeiboFrame];
} // 设置数据
- (void) calWeiboData {
Weibo *weibo = self.weiboFrame.weibo; // 1.头像
self.iconView.image = [UIImage imageNamed:weibo.icon]; // 2.昵称
self.nameView.text = weibo.name; // 3.vip标志
if (weibo.vip) {
self.vipView.hidden = NO;
}
else {
self.vipView.hidden = YES;
} // 4.博文
self.textView.text = weibo.text; // 5.配图
if (weibo.picture) {
self.pictureView.hidden = NO;
self.pictureView.image = [UIImage imageNamed:weibo.picture];
}
else {
self.pictureView.hidden = YES;
self.pictureView.image = nil;
}
} // 设置位置、尺寸
- (void) calWeiboFrame {
// 1.头像
self.iconView.frame = self.weiboFrame.iconFrame; // 2.昵称
self.nameView.frame = self.weiboFrame.nameFrame; // 3.vip标志
self.vipView.frame = self.weiboFrame.vipFrame; // 4.博文
self.textView.frame = self.weiboFrame.textFrame; // 5.配图
if (self.weiboFrame.weibo.picture) {
self.pictureView.frame = self.weiboFrame.pictureFrame;
}
} @end
 
 
Model:
 //
// Weibo.h
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// // 装在微博数据的model
#import <Foundation/Foundation.h> @interface Weibo : NSObject #pragma mark - 成员变量
/** 头像 */
@property(nonatomic, copy) NSString *icon; /** 昵称 */
@property(nonatomic, copy) NSString *name; /** vip标志 */
@property(nonatomic, assign) BOOL vip; /** 博文 */
@property(nonatomic, copy) NSString *text; /** 配图 */
@property(nonatomic, copy) NSString *picture; #pragma mark - 自定义初始化方法
/** 使用字典赋值成员 */
- (instancetype) initWithDictionary:(NSDictionary *) dictionary; /** 使用字典赋值成员 */
+ (instancetype) weiboWithDictionary:(NSDictionary *) dictionary; /** 返回空的model */
+ (instancetype) weibo; @end
 
 //
// Weibo.m
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "Weibo.h" @implementation Weibo /** 使用字典赋值成员 */
- (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dictionary];
} return self;
} /** 使用字典赋值成员 */
+ (instancetype) weiboWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
} /** 返回空的model */
+ (instancetype) weibo {
return [self weiboWithDictionary:nil];
} @end
 
 //
// WeiboFrame.h
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// // 装在了每个cell的位置、尺寸和微博数据的model @class Weibo;
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> // CGRect需要引入UIKit @interface WeiboFrame : NSObject // 微博数据
@property(nonatomic, strong) Weibo *weibo; /** 头像 */
@property(nonatomic, assign, readonly) CGRect iconFrame; /** 昵称 */
@property(nonatomic, assign, readonly) CGRect nameFrame; /** vip标志 */
@property(nonatomic, assign, readonly) CGRect vipFrame; /** 博文 */
@property(nonatomic, assign, readonly) CGRect textFrame; /** 配图 */
@property(nonatomic, assign, readonly) CGRect pictureFrame; /** 一条微博cell的高度 */
@property(nonatomic, assign, readonly) CGFloat cellHeight; @end
 //
// WeiboFrame.m
// Weibo
//
// Created by hellovoidworld on 14/12/5.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "WeiboFrame.h"
#import "Weibo.h" // 昵称字体
#define NAME_FONT [UIFont systemFontOfSize:14]
// 博文字体
#define TEXT_FONT [UIFont systemFontOfSize:15] @implementation WeiboFrame #pragma mark - 加载数据
// 加载数据,用以计算各个控件的位置、尺寸
- (void)setWeibo:(Weibo *)weibo {
_weibo = weibo; // 间隙参数
CGFloat padding = ; // 1.头像
CGFloat iconWidth = ;
CGFloat iconHeight = ;
CGFloat iconX = padding;
CGFloat iconY = padding;
_iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight); // 2.昵称
// 计算昵称占用的size
CGSize nameSize = [self calTextSizeWithText:self.weibo.name font:TEXT_FONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)]; CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding;
CGFloat nameY = iconY + (iconHeight - nameSize.height) / ;// 居中
_nameFrame.size = nameSize;
_nameFrame.origin = CGPointMake(nameX, nameY); // 3.vip标志
CGFloat vipWith = ;
CGFloat vipHeight = ;
CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding;
CGFloat vipY = nameY;
_vipFrame = CGRectMake(vipX, vipY, vipWith, vipHeight); // 4.博文
CGSize textSize = [self calTextSizeWithText:self.weibo.text font:TEXT_FONT maxSize:CGSizeMake(, MAXFLOAT)];
CGFloat textX = padding;
CGFloat textY = CGRectGetMaxY(_iconFrame) + padding;
_textFrame = CGRectMake(textX, textY, textSize.width, textSize.height); // 5.配图
if (self.weibo.picture) {
CGFloat pictureWidth = ;
CGFloat pictureHeight = ;
CGFloat pictureX = padding;
CGFloat pictureY = CGRectGetMaxY(_textFrame) + padding;
_pictureFrame = CGRectMake(pictureX, pictureY, pictureWidth, pictureHeight); _cellHeight = CGRectGetMaxY(_pictureFrame) + padding; //计算cell高度
}
else {
_cellHeight = CGRectGetMaxY(_textFrame) + padding;
}
} // 使用自带方法计算一段文字占用的size
- (CGSize) calTextSizeWithText:(NSString *) text font:(UIFont *) font maxSize:(CGSize) maxSize {
NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
} @end
 
weibo.plist:
 
images:
 
 
 
 
 
 

最新文章

  1. C语言内存分配方法。
  2. Elasticsearch 检索
  3. JUnit4参数的使用
  4. lucene-查询query-&gt;PrefixQuery使用前缀搜索
  5. 微软企业库5.0学习-Security.Cryptography模块
  6. ### C++总结-[类的继承]
  7. 数据库 —— mySQL的基本操作
  8. ARM指令和Thumb指令区别
  9. linux 从softnet_stat查看内核丢包信息
  10. Python学习之再议row_input
  11. 浏览器加载和渲染html的顺序(html/css/js)
  12. leetcode96
  13. Sketch网页截屏插件设计开发
  14. 写一个表达式检查所给的整数是否它第三个数字(从右向左)是7。示例:1732 -&gt; true。
  15. 【leetcode】409. Longest Palindrome
  16. java.io.Closeable 接口
  17. 关于DOM级别的一些问题,DOM0,DOM1,DOM2
  18. QYH练字
  19. UVALive 6910 Cutting Tree 并查集
  20. ajax劫持?

热门文章

  1. 深入理解ClassLoader(四)—类的父委托加载机制
  2. 使用JS动态创建含有1000行的表格
  3. codeforces #313 div1 C
  4. 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理
  5. Android四大基本组件
  6. while ((ch = getchar()) != EOF)中ch定义为char还是int型?cin、scanf等如何结束键盘输入
  7. NoSql数据库使用半年后在设计上面的一些心得 (转)
  8. Java String 的equals, == , hascode的区别
  9. Android中全屏或者取消标题栏
  10. C# 中的枚举类型 enum (属于值类型)