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 ...
随机推荐
- 微信小程序电商实战-首页(下)
好了,上一期我们把首页搜索.导航栏和广告轮播给做完了,那么接下来会继续完成我们首页的剩余部分,先看我们要实现的效果吧! 本期实现效果图.gif 本期我们要实现首页的实时热销榜.福利专场和左下方个人 ...
- Android数据绑定技术二,企业级开发
PS:上一篇文章写了Databinding的简单使用,写了一个绑定textview的示例,和绑定的一些用法,估计有的人会说,之前的写的好好的,为什么要数据绑定这样的写法呢,没办法,社会在进步,当然是怎 ...
- HTML5 笔记之 HTML5 的常见用法介绍
阅读目录 介绍 网页标题.文章标题.文章段落 介绍 字体大小.字体颜色.字体类型.字体位置.背景颜色 介绍 插入图片 介绍 网页内的超链接.网页间的超链接 介绍 有序列表.无序列表 介绍 表格制作 介 ...
- java中将数组、对象、Map、List转换成JSON数据
如果要将数组.对象.Map.List转换成JSON数据,那我们需要一些jar包: json-lib-2.4-jdk15.jar ezmorph-1.0.6.jar commons-logging.ja ...
- Ajax使用 初始化数据 + mvc
2017-3-16 mvc+jquery+easyUI做项目 <input type="text" id="txtSTQty" name="tx ...
- failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg"
https://stackoverflow.com/questions/43024337/why-this-error-when-i-try-to-create-workspaces-in-ros# ...
- tomcat7 的The Apache Tomcat Native library which allows optimal performance 的解决
1. 用Myeclipse启动tomcat7启动时可能会收到下面的信息: 七月 24, 2014 10:13:30 上午 org.apache.catalina.core.AprLife ...
- 2019.03.13 ZJOI2019模拟赛 解题报告
得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...
- 火车进出站(POJ1363)
题目链接:http://poj.org/problem?id=1363 #include <stdio.h> #include <stack> using namespace ...
- 轻量级HTTP服务器Nginx(常用配置实例)
轻量级HTTP服务器Nginx(常用配置实例) 文章来源于南非蚂蚁 Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...