1014-34-首页15-计算原创微博的frame------计算cell的高度---计算 UILabel 的 CGSize 的方法
一、总体思路:
在控制器中,每次拿到数据模型(请求了数据、加载新微博)的时候,就调用 - (NSArray *)stausFramesWithStatuses:(NSArray *)statuses, 将HWStatus模型转为HWStatusFrame模型,这个时候就完成了每一条微博(每一个cell )里面各个子控件以后要用到的 Frame 进行了计算,但是还没有将相应的 frame 赋值给相应的子控件的frame (那种赋值操作是在 UITableView代理 里面进行的: cell.statusFrame = self.statusFrames[indexPath.row]; ) 。代码如下:
/**
* 将HWStatus模型转为HWStatusFrame模型
*/
- (NSArray *)stausFramesWithStatuses:(NSArray *)statuses
{
NSMutableArray *frames = [NSMutableArray array];
for (HWStatus *status in statuses) {
HWStatusFrame *f = [[HWStatusFrame alloc] init];
f.status = status; // 在 - (void)setStatus:(HWStatus *)status 完成了每一条微博(每一个cell )里面各个子控件的 Frame 的计算
[frames addObject:f];
}
return frames;
}
二、完整代码:
---------------------------HWHomeViewController.m---------------------------------------------
/**
* 微博数组(里面放的都是HWStatusFrame模型,一个HWStatusFrame对象就代表一条微博)
*/
@property (nonatomic, strong) NSMutableArray *statusFrames;
@end
@implementation HWHomeViewController
- (NSMutableArray *)statusFrames
{
if (!_statusFrames) {
self.statusFrames = [NSMutableArray array];
}
return _statusFrames;
}
/**
* UIRefreshControl进入刷新状态:加载最新的数据
*/
- (void)loadNewStatus:(UIRefreshControl *)control
{
// 1.请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.拼接请求参数
HWAccount *account = [HWAccountTool account];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = account.access_token;
// 取出最前面的微博(最新的微博,ID最大的微博)
HWStatusFrame *firstStatusF = [self.statusFrames firstObject];
if (firstStatusF) {
// 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0
params[@"since_id"] = firstStatusF.status.idstr;
}
// 3.发送请求
[mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
HWLog(@"%@", responseObject);
// 将 "微博字典"数组 转为 "微博模型"数组
NSArray *newStatuses = [HWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
// 将 HWStatus数组 转为 HWStatusFrame数组
NSArray *newFrames = [self stausFramesWithStatuses:newStatuses];
// 将最新的微博数据,添加到总数组的最前面
NSRange range = NSMakeRange(0, newFrames.count);
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
[self.statusFrames insertObjects:newFrames atIndexes:set]; // statusFrames 这个是 数组,存放每一条微博的 frame模型
// 刷新表格
[self.tableView reloadData];
// 结束刷新
[control endRefreshing];
// 显示最新微博的数量
[self showNewStatusCount:newStatuses.count];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HWLog(@"请求失败-%@", error);
// 结束刷新刷新
[control endRefreshing];
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 获得cell
HWStatusCell *cell = [HWStatusCell cellWithTableView:tableView];
// 给cell传递模型数据, 在 setStatusFrame 里面完成 对各个子控件的 frame 赋值和 数据赋值。
cell.statusFrame = self.statusFrames[indexPath.row];
return cell;
}
---------------------------HWStatus.h---------------------------------------------
// HWStatus.h
//
// Created by apple on 14-10-12.
// Copyright (c) 2014年 heima. All rights reserved.
// 微博模型
#import <Foundation/Foundation.h>
@class HWUser;
@interface HWStatus : NSObject
/** string 字符串型的微博ID*/
@property (nonatomic, copy) NSString *idstr;
/** string 微博信息内容*/
@property (nonatomic, copy) NSString *text;
/** object 微博作者的用户信息字段 详细*/
@property (nonatomic, strong) HWUser *user;
/** string 微博创建时间*/
@property (nonatomic, copy) NSString *created_at;
/** string 微博来源*/
@property (nonatomic, copy) NSString *source;
@end
---------------------------HWStatus.m---------------------------------------------
// HWStatus.m
//
// Created by apple on 14-10-12.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HWStatus.h"
@implementation HWStatus
@end
---------------------------HWStatusFrame.h---------------------------------------------
//
// HWStatusFrame.h
//
// Created by apple on 14-10-14.
// Copyright (c) 2014年 heima. All rights reserved.
// 一个HWStatusFrame模型里面包含的信息
// 1.存放着一个cell内部所有子控件的frame数据
// 2.存放一个cell的高度
// 3.存放着一个数据模型HWStatus
#import <Foundation/Foundation.h>
// 昵称字体
#define HWStatusCellNameFont [UIFont systemFontOfSize:15]
// 时间字体
#define HWStatusCellTimeFont [UIFont systemFontOfSize:12]
// 来源字体
#define HWStatusCellSourceFont HWStatusCellTimeFont
// 正文字体
#define HWStatusCellContentFont [UIFont systemFontOfSize:14]
@class HWStatus;
@interface HWStatusFrame : NSObject
@property (nonatomic, strong) HWStatus *status;
/** 原创微博整体 */
@property (nonatomic, assign) CGRect originalViewF;
/** 头像 */
@property (nonatomic, assign) CGRect iconViewF;
/** 会员图标 */
@property (nonatomic, assign) CGRect vipViewF;
/** 配图 */
@property (nonatomic, assign) CGRect photoViewF;
/** 昵称 */
@property (nonatomic, assign) CGRect nameLabelF;
/** 时间 */
@property (nonatomic, assign) CGRect timeLabelF;
/** 来源 */
@property (nonatomic, assign) CGRect sourceLabelF;
/** 正文 */
@property (nonatomic, assign) CGRect contentLabelF;
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
---------------------------HWStatusFrame.m---------------------------------------------
// HWStatusFrame.m
//
// Created by apple on 14-10-14.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HWStatusFrame.h"
#import "HWStatus.h"
#import "HWUser.h"
// cell的边框宽度
#define HWStatusCellBorderW 10
@implementation HWStatusFrame
// 通过出入文字的 text 和 font 计算 UILabel 的 CGSize 的方法
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = font;
CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
// 通过出入文字的 text 计算 UILabel 的 高度 (最大宽度设置为无限大了)
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font
{
return [self sizeWithText:text font:font maxW:MAXFLOAT];
}
- (void)setStatus:(HWStatus *)status
{
_status = status;
HWUser *user = status.user;
// cell的宽度
CGFloat cellW = [UIScreen mainScreen].bounds.size.width;
/* 原创微博 */
/** 头像 */
CGFloat iconWH = 35;
CGFloat iconX = HWStatusCellBorderW;
CGFloat iconY = HWStatusCellBorderW;
self.iconViewF = CGRectMake(iconX, iconY, iconWH, iconWH);
/** 昵称 */
CGFloat nameX = CGRectGetMaxX(self.iconViewF) + HWStatusCellBorderW;
CGFloat nameY = iconY;
CGSize nameSize = [self sizeWithText:user.name font:HWStatusCellNameFont];
self.nameLabelF = (CGRect){{nameX, nameY}, nameSize};
/** 会员图标 */
if (user.isVip) {
CGFloat vipX = CGRectGetMaxX(self.nameLabelF) + HWStatusCellBorderW;
CGFloat vipY = nameY;
CGFloat vipH = nameSize.height;
CGFloat vipW = 14;
self.vipViewF = CGRectMake(vipX, vipY, vipW, vipH);
}
/** 时间 */
CGFloat timeX = nameX;
CGFloat timeY = CGRectGetMaxY(self.nameLabelF) + HWStatusCellBorderW;
CGSize timeSize = [self sizeWithText:status.created_at font:HWStatusCellTimeFont];
self.timeLabelF = (CGRect){{timeX, timeY}, timeSize};
/** 来源 */
CGFloat sourceX = CGRectGetMaxX(self.timeLabelF) + HWStatusCellBorderW;
CGFloat sourceY = timeY;
CGSize sourceSize = [self sizeWithText:status.source font:HWStatusCellSourceFont];
self.sourceLabelF = (CGRect){{sourceX, sourceY}, sourceSize};
/** 正文 */
CGFloat contentX = iconX;
CGFloat contentY = MAX(CGRectGetMaxY(self.iconViewF), CGRectGetMaxY(self.timeLabelF)) + HWStatusCellBorderW;
CGFloat maxW = cellW - 2 * contentX;
CGSize contentSize = [self sizeWithText:status.text font:HWStatusCellContentFont maxW:maxW];
self.contentLabelF = (CGRect){{contentX, contentY}, contentSize};
/** 配图 */
/** 原创微博整体 */
CGFloat originalX = 0;
CGFloat originalY = 0;
CGFloat originalW = cellW;
CGFloat originalH = CGRectGetMaxY(self.contentLabelF) + HWStatusCellBorderW;
self.originalViewF = CGRectMake(originalX, originalY, originalW, originalH);
self.cellHeight = CGRectGetMaxY(self.originalViewF);
}
@end
---------------------------HWStatusCell.h---------------------------------------------
//
// HWStatusCell.h
// 黑马微博2期
//
// Created by apple on 14-10-14.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import <UIKit/UIKit.h>
@class HWStatusFrame;
@interface HWStatusCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) HWStatusFrame *statusFrame;
@end
---------------------------HWStatusCell.m---------------------------------------------
// HWStatusCell.m
//
// Created by apple on 14-10-14.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HWStatusCell.h"
#import "HWStatus.h"
#import "HWUser.h"
#import "HWStatusFrame.h"
#import "UIImageView+WebCache.h"
@interface HWStatusCell()
/* 原创微博 */
/** 原创微博整体 */
@property (nonatomic, weak) UIView *originalView;
/** 头像 */
@property (nonatomic, weak) UIImageView *iconView;
/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipView;
/** 配图 */
@property (nonatomic, weak) UIImageView *photoView;
/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 正文 */
@property (nonatomic, weak) UILabel *contentLabel;
@end
@implementation HWStatusCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"status";
HWStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[HWStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}
/**
* cell的初始化方法,一个cell只会调用一次
* 一般在这里添加所有可能显示的子控件,以及子控件的一次性设置
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
/** 原创微博整体 */
UIView *originalView = [[UIView alloc] init];
[self.contentView addSubview:originalView];
self.originalView = originalView;
/** 头像 */
UIImageView *iconView = [[UIImageView alloc] init];
[originalView addSubview:iconView];
self.iconView = iconView;
/** 会员图标 */
UIImageView *vipView = [[UIImageView alloc] init];
vipView.contentMode = UIViewContentModeCenter;
[originalView addSubview:vipView];
self.vipView = vipView;
/** 配图 */
UIImageView *photoView = [[UIImageView alloc] init];
[originalView addSubview:photoView];
self.photoView = photoView;
/** 昵称 */
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.font = HWStatusCellNameFont;
[originalView addSubview:nameLabel];
self.nameLabel = nameLabel;
/** 时间 */
UILabel *timeLabel = [[UILabel alloc] init];
timeLabel.font = HWStatusCellTimeFont;
[originalView addSubview:timeLabel];
self.timeLabel = timeLabel;
/** 来源 */
UILabel *sourceLabel = [[UILabel alloc] init];
sourceLabel.font = HWStatusCellSourceFont;
[originalView addSubview:sourceLabel];
self.sourceLabel = sourceLabel;
/** 正文 */
UILabel *contentLabel = [[UILabel alloc] init];
contentLabel.font = HWStatusCellContentFont;
contentLabel.numberOfLines = 0;
[originalView addSubview:contentLabel];
self.contentLabel = contentLabel;
}
return self;
}
// 对各个子控件 设置 Frame 和 赋值数据
- (void)setStatusFrame:(HWStatusFrame *)statusFrame
{
_statusFrame = statusFrame;
HWStatus *status = statusFrame.status;
HWUser *user = status.user;
/** 原创微博整体 */
self.originalView.frame = statusFrame.originalViewF;
/** 头像 */
self.iconView.frame = statusFrame.iconViewF;
[self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
/** 会员图标 */
if (user.isVip) {
self.vipView.hidden = NO;
self.vipView.frame = statusFrame.vipViewF;
NSString *vipName = [NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank];
self.vipView.image = [UIImage imageNamed:vipName];
self.nameLabel.textColor = [UIColor orangeColor];
} else {
self.nameLabel.textColor = [UIColor blackColor];
self.vipView.hidden = YES;
}
/** 配图 */
self.photoView.frame = statusFrame.photoViewF;
self.photoView.backgroundColor = [UIColor redColor];
/** 昵称 */
self.nameLabel.text = user.name;
self.nameLabel.frame = statusFrame.nameLabelF;
/** 时间 */
self.timeLabel.text = status.created_at;
self.timeLabel.frame = statusFrame.timeLabelF;
/** 来源 */
self.sourceLabel.text = status.source;
self.sourceLabel.frame = statusFrame.sourceLabelF;
/** 正文 */
self.contentLabel.text = status.text;
self.contentLabel.frame = statusFrame.contentLabelF;
}
@end
1014-34-首页15-计算原创微博的frame------计算cell的高度---计算 UILabel 的 CGSize 的方法的更多相关文章
- 新浪微博客户端(24)-计算原创微博配图frame
DJStatus.h #import <Foundation/Foundation.h> @class DJUser; /** 微博 */ @interface DJStatus : NS ...
- UITableViewCell 高度计算从混沌初始到天地交泰
[原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...
- 【原创 Hadoop&Spark 动手实践 7】Spark 计算引擎剖析与动手实践
[原创 Hadoop&Spark 动手实践 7]Spark计算引擎剖析与动手实践 目标: 1. 理解Spark计算引擎的理论知识 2. 动手实践更深入的理解Spark计算引擎的细节 3. 通过 ...
- [iOS微博项目 - 4.1] - cell的frame模型
github: https://github.com/hellovoidworld/HVWWeibo A.cell的frame模型设计 1.需求 每个cell都有一个frame实例引用 frame模型 ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- 优化UITableViewCell高度计算的那些事(RunLoop)
这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...
- iOS开发之计算动态cell的高度并缓存
项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...
- 《转》优化UITableViewCell高度计算的那些事
我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...
随机推荐
- springmvc+spring+mybatis+sqlserver----插入一条新数据
<insert id="addOneMsg" parameterType="java.util.Map"> INSERT INTO PDA_JWL_ ...
- Python常用模块(二)
一.json与pickle json与pickle模块是为了完成数据的序列化. 序列化是指把对象(变量)从内存中变成可存储或传输的过程,在Python中叫picking,在其他语言中也由其他的叫法,但 ...
- 从零开始的全栈工程师——js篇(js的异步)
js中的异步 Javascript语言的执行环境是"单线程"(single thread,就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任 ...
- MyBatis之会话Session原理
MyBatis 之会话 Session 执行逻辑 1.SQL 会话工厂构建器类 SqlSessionFactoryBuilder 的 build 方法用于构建 SqlSessionFactory 类的 ...
- jQuery实现焦点图[兼容ie7+]
HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...
- 【起航计划 029】2015 起航计划 Android APIDemo的魔鬼步伐 28 App->Preferences->Default Values 偏好默认值
DefaultValues 介绍了如何在XML中定义Preference的缺省值. <CheckBoxPreference android:key="default_checkbox& ...
- msql 综合练习
8.统计列印各科成绩,各分数段人数: 课程ID,课程名称,[100-85],[85-70],[70-60],[<60] 尽管表面看上去不那么容易,其实用 CASE 可以很容易地实现: SELE ...
- 【BZOJ1453】[WC] Dface双面棋盘(LCT维护联通块个数)
点此看题面 大致题意: 给你一个\(n*n\)的黑白棋盘,每次将一个格子翻转,分别求黑色连通块和白色连通块的个数. \(LCT\)动态维护图连通性 关于这一部分内容,可以参考这道例题:[BZOJ402 ...
- win10 下的python虚拟环境安装使用(使用powershell)
安装virtualenv 若要使用python虚拟环境进行开发,首先需要安装virtualenv.命令:pip install virtualenv 我已经装过了
- 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术
第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...