*****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. Uva442 hdu 1082 Matrix Chain Multiplication

    要注意取出来的时候 先取出q的是后面那个矩阵 后取出p的是前面的矩阵 所以是判断 p.a == q.b #include <iostream> #include <stack> ...

  2. mac OS X操作 -- 常用

    显示/隐藏默认隐藏文件:defaults write com.apple.finder AppleShowAllFiles -bool true/false 重置ROOT密码: http://www. ...

  3. PHP历程(封装的增删改查方法)

    db.class.php   主要方法 <?php /** * 数据库配置信息 */ define('DB_HOST','127.0.0.1'); //服务器 define('DB_USER', ...

  4. Eclipse快捷键/快捷操作汇总

    1.建立.切换不同的工作空间: 工作空间是放置项目的,它是项目的集合,多个工程放在一个工作空间上容易出问题,建议把不同项目存放在单独的工作  空间内,让项目代码更加有序 file → switch w ...

  5. oracle视图

    视图 SELECT -- int.rowner "rgroup owner", -- int.rname "refresh group", mv.owner a ...

  6. ural 2063. Black and White

    2063. Black and White Time limit: 1.0 secondMemory limit: 64 MB Let’s play a game. You are given a r ...

  7. unity 解析tmx

    using UnityEngine; using System.Collections; using System.IO; using System.Xml; public class xml : M ...

  8. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  9. [Leetcode] Word BreakII

    Question: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence w ...

  10. URAL 1635. Mnemonics and Palindromes(DP)

    题目链接 本来用区间DP,3次方的复杂度,T了,看了看题解,降维,直接二次方的复杂度可以解.然后折腾一下输出路径..终于过了. #include <cstring> #include &l ...