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

***********

#import "HMViewController.h"
#import "HMStatus.h"
#import "HMStatusCell.h"
#import "HMStatusFrame.h"

@interface HMViewController ()
/** 保存statusFrame模型的数组 */
@property (nonatomic, strong) NSArray *statusFrames;
@end

@implementation HMViewController
static NSString *ID = @"Cell";

- (NSArray *)statusFrames
{
    if (_statusFrames == nil) _statusFrames = [HMStatusFrame statusFrames];
    return _statusFrames;
}

// 界面创建完成被调用
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 为tableView注册可重用单元格
    [self.tableView registerClass:[HMStatusCell class] forCellReuseIdentifier:ID];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statusFrames.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     在Storyboard中指定了可重用标示符,同时指定了Cell的类是HMStatusCell
     
     系统会为tableView注册一个原形Cell,专门用来做可重用单元格的,一旦缓冲区中不存在
     可重用单元格,系统会使用原形Cell新实例化一个Cell用程序使用!
     
     因此如果在,Storyboard中,注册了原形Cell,就不再需要 cell == nil的判断了
     */
//    HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
    // 使用这个方法,要求一定注册可重用单元格,否则就会崩溃!
    // 官方建议使用以下方法,利用程序的崩溃,及时发现问题
    HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    // 一旦在注册了可重用Cell,以上两个方法是等价的
    
//    if (cell == nil) {
//        cell = [[HMStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }

    // 赋值
    // 取出StatusFrame模型
    HMStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    cell.statusFrame = statusFrame;
    
    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

 

********modol.h

#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

 


****************

#import "HMStatusFrame.h"
#import "HMStatus.h"
#import "NSString+Tools.h"

/** 姓名字体 */
#define kNameFont   [UIFont systemFontOfSize:14]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:16]

@implementation HMStatusFrame
//@synthesize iconF = _iconF;

/** 
 一旦重写了readonly属性的getter方法,_的成员变量就不存在了 
 
 如果还需要使用_成员变量,则需要使用@synthesize生成对应的成员变量
 */
//- (CGRect)iconF
//{
//    
//}

- (void)setStatus:(HMStatus *)status
{
    _status = status;
    
    // 0. 定义间距
    CGFloat padding = 10;
    
    // 1. 头像
    CGFloat iconX = padding;
    CGFloat iconY = padding;
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    _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];
    CGRect nameFrame = [self.status.name textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:nameDict];
    
    nameFrame.origin.x = CGRectGetMaxX(self.iconF) + padding;
    nameFrame.origin.y = padding + (self.iconF.size.height - nameFrame.size.height) * 0.5;
    _nameF = nameFrame;
    
    // vip图标
    CGFloat vipX = CGRectGetMaxX(self.nameF) + padding;
    CGFloat vipY = self.nameF.origin.y;
    CGFloat vipW = 14;
    CGFloat vipH = 14;
    _vipF = CGRectMake(vipX, vipY, vipW, vipH);
    
    // 正文
    NSDictionary *textDict = @{NSFontAttributeName: kTextFont};
//    CGRect textFrame = [self.status.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
    CGRect textFrame = [self.status.text textRectWithSize:CGSizeMake(300, MAXFLOAT) attributes:textDict];
    
    textFrame.origin.x = padding;
    textFrame.origin.y = CGRectGetMaxY(self.iconF) + padding;
    _textF = textFrame;
    
    if (self.status.picture.length > 0) {
        // 配图
        CGFloat pictureX = padding;
        CGFloat pictureY = CGRectGetMaxY(textFrame) + padding;
        CGFloat pictureW = 100;
        CGFloat pictureH = 100;
        _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
        
        _cellHeight = CGRectGetMaxY(self.pictureF) + padding;
    } else {
        _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];
        
        // 调用自己的setter方法,保存status数据模型,同时计算出所有控件的位置
        statusFrame.status = status;
        
        // 将statusFrame添加到数组
        [arrayM addObject:statusFrame];
    }
    
    return arrayM;
}


@end


*************cell.h

#import <UIKit/UIKit.h>
@class HMStatusFrame;

@interface HMStatusCell : UITableViewCell
@property (nonatomic, strong) HMStatusFrame *statusFrame;
@end

*************cell.m

#import "HMStatusCell.h"
#import "HMStatus.h"
#import "HMStatusFrame.h"

/** 姓名字体 */
#define kNameFont   [UIFont systemFontOfSize:14]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:16]

@interface HMStatusCell()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameView;
@property (nonatomic, strong) UIImageView *vipView;
@property (nonatomic, strong) UILabel *textView;
@property (nonatomic, strong) UIImageView *pictureView;

@end

@implementation HMStatusCell

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:_iconView];
    }
    return _iconView;
}

- (UILabel *)nameView
{
    if (_nameView == nil) {
        _nameView = [[UILabel alloc] init];
        // 默认字体是17号
        _nameView.font = kNameFont;
        [self.contentView addSubview:_nameView];
    }
    return _nameView;
}

- (UIImageView *)vipView
{
    if (_vipView == nil) {
        _vipView = [[UIImageView alloc] init];
        _vipView.image = [UIImage imageNamed:@"vip"];
        _vipView.hidden = YES;
        
        [self.contentView addSubview:_vipView];
    }
    return _vipView;
}

- (UILabel *)textView
{
    if (_textView == nil) {
        _textView = [[UILabel alloc] init];
        _textView.font = kTextFont;
        _textView.numberOfLines = 0;
        
        [self.contentView addSubview:_textView];
    }
    return _textView;
}

- (UIImageView *)pictureView
{
    if (_pictureView == nil) {
        _pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

- (void)setStatusFrame:(HMStatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    
    // 1> 设置数据
    [self settingData];
    
    // 2> 设置位置
    [self settingFrame];
}

/** 设置数据 */
- (void)settingData
{
    HMStatus *status = self.statusFrame.status;
    
    // 头像
    self.iconView.image = [UIImage imageNamed:status.icon];
    // 姓名
    self.nameView.text = status.name;
    // vip(可选的)
    if (status.vip) {
        self.vipView.hidden = NO;
        self.nameView.textColor = [UIColor redColor];
    } else {
        self.vipView.hidden = YES;
        self.nameView.textColor = [UIColor blackColor];
    }
    
    // 正文
    self.textView.text = status.text;
    
    // 配图(可选参数)
    // imageNamed:nil CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
    if (status.picture.length > 0) {
        self.pictureView.hidden = NO;
        self.pictureView.image = [UIImage imageNamed:status.picture];
    } else {
        self.pictureView.hidden = YES;
    }
}

/** 设置位置 */
- (void)settingFrame
{
    // 1. 头像
    self.iconView.frame = self.statusFrame.iconF;
    
    // 2. 姓名大小由文字的长度来决定
    self.nameView.frame = self.statusFrame.nameF;
    
    // vip图标
    self.vipView.frame = self.statusFrame.vipF;
    
    // 正文
    self.textView.frame = self.statusFrame.textF;
    
    // 配图
    if (self.statusFrame.status.picture.length > 0) {
        self.pictureView.frame = self.statusFrame.pictureF;
    }
}

@end

 

posted @ 2015-08-11 17:36  iso  阅读(422)  评论(0编辑  收藏  举报