*****HMViewController

#import "HMViewController.h"
#import "HMStatus.h"
#import "HMStatusCell.h"
#import "HMStatusFrame.h" @interface HMViewController ()
/** 保存statusFrame模型的数组 */
@property (nonatomic, strong) NSArray *statusFrames;
@end @implementation HMViewController - (NSArray *)statusFrames
{
if (_statusFrames == nil) _statusFrames = [HMStatusFrame statusFrames];
return _statusFrames;
} - (void)viewDidLoad
{
[super viewDidLoad]; //self.tableView.rowHeight = 200;
} #pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusFrames.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) {
cell = [[HMStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 赋值
// 取出StatusFrame模型
HMStatusFrame *statusFrame = self.statusFrames[indexPath.row];
cell.status = statusFrame.status; //4 中 略 return cell;
} #pragma mark - 代理方法
/** 计算单元格行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
计算行高的方法,会在加载表格数据时,有多少行计算多少次 contentSize 问题:此方法执行的时候,cell还没有被实例化!
但是:行高计算是在实例化cell时,通过设置status属性,计算的=>有了status模型,就可以知道行高! 问题:如何在cell实例化之前,获得行高?
解决方法:通过status可以计算得到行高!=》再建立一个模型,专门计算所有控件的位置
*/
HMStatusFrame *statusFrame = self.statusFrames[indexPath.row]; return statusFrame.cellHeight;
} @end

****HMStatusFrame

#import <Foundation/Foundation.h>
@class HMStatus; /** 专门计算所有控件位置 */
@interface HMStatusFrame : NSObject
@property (nonatomic, assign) CGRect iconF;
@property (nonatomic, assign) CGRect nameF;
@property (nonatomic, assign) CGRect vipF;
@property (nonatomic, assign) CGRect textF;
@property (nonatomic, assign) CGRect pictureF; /** 行高 */
@property (nonatomic, assign) CGFloat cellHeight; /** 所有控件的尺寸都可以通过Status来计算得出 */
@property (nonatomic, strong) HMStatus *status; /** 所有的statusFrame数据数组 */
+ (NSArray *)statusFrames; @end

****************HMStatusFrame.m

#import "HMStatusFrame.h"
#import "HMStatus.h" /** 姓名字体 */
#define kNameFont [UIFont systemFontOfSize:14]
/** 正文字体 */
#define kTextFont [UIFont systemFontOfSize:16] @interface HMStatusFrame () @end
@implementation HMStatusFrame - (void)setStatus:(HMStatus *)status
{
_status = status; // 0. 定义间距
CGFloat padding = ; // 1. 头像
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = ;
CGFloat iconH = ;
self.iconF = CGRectMake(iconX, iconY, iconW, iconH); // 2. 姓名大小由文字的长度来决定
// boundingRectWithSize计算给定文本字符串所占的区域
// 返回值是一个x,y = 0的CGRect,w,h是计算好的宽高
//
// 如果要计算多行的准确高度,需要传入NSStringDrawingUsesLineFragmentOrigin选项
// dict用于指定字体的相关属性的字典,UIKit框架中的第一个头文件
// context: nil
NSDictionary *nameDict = @{NSFontAttributeName: kNameFont};
CGRect nameFrame = [self.status.name boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil];
nameFrame.origin.x = CGRectGetMaxX(self.iconF) + padding;
nameFrame.origin.y = padding + (self.iconF.size.height - nameFrame.size.height) * 0.5;
self.nameF = nameFrame; // vip图标
CGFloat vipX = CGRectGetMaxX(self.nameF) + padding;
CGFloat vipY = self.nameF.origin.y;
CGFloat vipW = ;
CGFloat vipH = ;
self.vipF = CGRectMake(vipX, vipY, vipW, vipH); // 正文
NSDictionary *textDict = @{NSFontAttributeName: kTextFont};
CGRect textFrame = [self.status.text boundingRectWithSize:CGSizeMake(, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
textFrame.origin.x = padding;
textFrame.origin.y = CGRectGetMaxY(self.iconF) + padding;
self.textF = textFrame; if (self.status.picture.length > ) {
// 配图
CGFloat pictureX = padding;
CGFloat pictureY = CGRectGetMaxY(textFrame) + padding;
CGFloat pictureW = ;
CGFloat pictureH = ;
self.pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH); self.cellHeight = CGRectGetMaxY(self.pictureF) + padding;
} else {
self.cellHeight = CGRectGetMaxY(self.textF) + padding;
}
} + (NSArray *)statusFrames
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
// 要添加statusFrame对象
HMStatusFrame *statusFrame = [[HMStatusFrame alloc] init]; // 实例化一个新的Status模型
HMStatus *status = [HMStatus statusWithDict:dict]; //4中 略 // 调用自己的setter方法,保存status数据模型,同时计算出所有控件的位置
statusFrame.status = status; // 将statusFrame添加到数组
[arrayM addObject:statusFrame];
} return arrayM;
} @end

IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)的更多相关文章

  1. IOS第八天(7:UITableViewController新浪微博,cell 复用的简单写法优化和cell高度从模型中获取)

    *********** #import "HMViewController.h" #import "HMStatus.h" #import "HMSt ...

  2. IOS第八天(5:UITableViewController新浪微博, 计算行高)

    在 4 的 基础上重写 以下的方法 control #pragma mark - 代理方法 /** 计算单元格行高 */ - (CGFloat)tableView:(UITableView *)tab ...

  3. IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)

    ******控制control #import "HMViewController.h" #import "HMStatus.h" #import " ...

  4. iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)

    今天博客中,我们就来实现一下一些常用资讯类App中常用的分类选择的控件的封装.本篇博客中没有使用到什么新的技术点,如果非得说用到了什么新的技术点的话,那么勉强的说,用到了一些iOS9以后UIColle ...

  5. 【IOS界面布局】横竖屏切换和控件自适应(推荐)

    [IOS界面布局]横竖屏切换和控件自适应(推荐) 分类: [MAC/IOS下开发]2013-11-06 15:14 8798人阅读 评论(0) 收藏 举报 横竖屏切换 自适应 第一种:通过人为的办法改 ...

  6. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  7. atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系

    atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系 1. server控件是要server了解了标签.种类型的server控件: 1 1. ...

  8. iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示

    文本将会实现把数据存储到plist文件里.然后在程序中进行读取.在TableView控件中依据不同的类别显示Section. 有关TableView 的其它实现,请參考<iOS项目开发实战--学 ...

  9. tabBarItem是模型,只有控件才有textColor属性

    如果通过模型设置控件的文字颜色,只能通过文本属性(富文本:颜色,字体,图文混排,空心)

随机推荐

  1. ReportViewer报表

    个人感觉ReportViewer>DataGridView>listView 打开一个空的winform窗体程序,工具栏报表拖入 ReportViewer 在空的Form1中 在同一命名空 ...

  2. 豆制品厂开车超市送货智能手持PDA POS打票机-手持票据打印机

    豆制品厂开车拉着豆腐到某一个超市送货,到达后秤出斤数后就用票据打印机开单 能直接开单,单子一式两份,一张给客户一张留底,到月底时客户要根据客户的量返点的,单子统计.能现场开单,单子上显示哪个超市,豆制 ...

  3. BZOJ 1901 Zju2112 Dynamic Rankings ——整体二分

    [题目分析] 上次用树状数组套主席树做的,这次用整体二分去水. 把所有的查询的结果一起进行二分,思路很好. [代码] #include <cstdio> #include <cstr ...

  4. OAuth2授权页面state参数的设计

    场景描述: 第三方应用:freeshare 账号系统:account freeshare点击登录,跳转到account页面进行授权. 具体例子可以参考我们的freeshare系统:http://fre ...

  5. POJ3680 Intervals(最小费用最大流)

    选择若干条线段使权值最大,并且点覆盖次数不超过k. 建图如下:vs到0建立容量为k费用为0的边:坐标终点到vt连接一条容量为k费用为0的边:对于每两个相邻坐标连接一条容量为INF费用为0的边:对于线段 ...

  6. BZOJ4527 : K-D-Sequence

    先把所有数减去最小值,防止负数出现问题. $d=0$,直接$O(n)$扫过去即可. $d\neq 0$,首先通过双指针求出每个数作为右端点时往左可以延伸到哪里,中间任意两个数差值都是$d$的倍数且不重 ...

  7. [转]linux下svn命令大全

    FROM:http://www.jb51.net/os/RedHat/2461.html 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:s ...

  8. 原生js添加和删除类

    原生js添加和删除类: this.className +=" "; this.className = this.className.replace(" 原来的类" ...

  9. Codeforces Round #246 (Div. 2) B. Football Kit

    题目的意思是求出每个队穿主场衣服和客场衣服的次数 每个队作为主场的次数是n-1,作为客场的次数是n-1 当每个队打主场的时候肯定穿的主场衣服 当每个队打客场时,如果客场与主场的衣服不同,则穿客场衣服 ...

  10. 10秒钟sublime text 3安装SVN插件

    注意:此处我提前已经安装了towerSVN,你可能需要提前安装好 towerSVN,之前安装redis之后我才明白,安装插件时安装软件好像 是一个必要的步骤,也就是说安装插件只是让你能在这里使用你已 ...