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 ...
随机推荐
- Java基础入门 - 三种注释及文档注释详解
类似C/C++,Java也支持单行和多行注释 注释中的字符在编译时会被忽略 注释通常为类.变量和方法的主要描述 单行注释 // 注释内容 多行注释 /* 注释内容 */ /* * 注释内容 */ 文档 ...
- Python中的基本数据类型之列表与元组初步了解
一.什么是列表 1.列表是Python中的基本数据类型之一用[]来表示,每一项元素由逗号隔开,列表什么都能装,(能装对象的对象) 2.可以用来存放大量数据 3.是一个可以改变的数据类型 二.列表的索引 ...
- 我的第一台 Mac
我的第一台 Mac 心里的这棵草长了一年多之后终于狠心剁手了. 2018年11月6号下单 2018 款 MBP 2.6/32/512 定制款,7号早上到手 --- 感受(个人主观感觉-)
- 移动端纯CSS3制作圆形进度条所遇到的问题
近日在开发的页面中,需要制作一个动态的圆形进度条,首先想到的是利用两个矩形,宽等于直径的一半,高等于直径,两个矩形利用浮动贴在一起,设置overflow:hidden属性,作为盒子,内部有一个与其宽高 ...
- 浅谈SQL Server中的事务日志(一)----事务日志的物理和逻辑构架
简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...
- ansible使用2-inventory & dynamic inventory
默认位置 /etc/ansible/hosts 标准 mail.example.com [webservers] foo.example.com bar.example.com [dbservers] ...
- vue-表单绑定
表单数据绑定1.1你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖,它负责监听用户的输 ...
- R cannot be resolved的几种可能 R not generated
项目又爆红了,Eclipse真是够操心,顺便看一下R cannot be resolved的几种可能 这次是SVN合并的问题 2015-12-24 主要看 Console 输出的问题位置即可,一般都是 ...
- thinkphp 去掉URL 里面的index.php
例如你的原路径是 http://localhost/test/index.php/home/goods/index.html 那么现在的地址是 http://localhost/test/home/g ...
- jQuery实现网页右下角悬浮层提示
最近有同事提到类似网页右下角的消息悬浮提示框的制作.我之前也做过一个类似的例子,很简单.是仿QQ消息.现在感觉之前的那个例子只是说了实现原理,整体上给你的感觉还是太丑,今天为大家带来一个新的例子.是D ...