IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)

时间:2023-03-08 20:20:48
IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)

*****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