您的位置:首页 > 移动开发 > IOS开发

ios-QQ界面(利用新浪微博方法实现,消除新浪微博重复计算的缺点)

2015-08-27 19:58 686 查看
消除重复计算的缺点,既是将有两个模型,一个数据模型,一个位置模型。位置模型里面包含了数据模型,将计算位置和数据都一起做了。

Constant.h文件保存常量


//
//  Constant.h
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#ifndef QQ___Constant_h
#define QQ___Constant_h

#define bScreenWidth [[UIScreen mainScreen] bounds].size.width

#define bNormalH 44

#define bIconW 50

#define bIconH 50

#define bBtnFont [UIFont systemFontOfSize:15.0f]

#endif


//
//  HMMessageCell.h
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>
//@class HMMessageModel;
@class HMMessageFrameModel;
@interface HMMessageCell : UITableViewCell

+ (instancetype)messageCellWithTableView:(UITableView *)tableview;

//@property(nonatomic,strong) HMMessageModel *messageModel;

// frame 的模型
@property(nonatomic,strong) HMMessageFrameModel *frameModel;

@end


把懒加载放到 视图文件cell里面,同时cell里面只有位置了

//
//  HMMessageCell.m
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMMessageCell.h"
#import "HMMessageModel.h"
#import "HMMessageFrameModel.h"
#import "Constant.h"

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

@interface HMMessageCell()
@property(nonatomic,strong)UILabel *time;
@property(nonatomic,strong)UIButton *textView;
@property(nonatomic,strong)UIImageView *icon;
@end
@implementation HMMessageCell

+ (instancetype)messageCellWithTableView:(UITableView *)tableview
{
static NSString *ID = @"messageCell";
HMMessageCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}

return cell;
}

- (UILabel *)time
{
if (_time ==nil) {
_time = [[UILabel alloc]init];
_time.textAlignment = NSTextAlignmentCenter;
_time.font = [UIFont systemFontOfSize:13.0f];
[self.contentView addSubview:_time];
}
return _time;
}

- (UIButton *)textView
{
if (_textView == nil) {
_textView = [[UIButton alloc]init];
_textView.backgroundColor = [UIColor grayColor];
_textView.titleLabel.font = bBtnFont;
_textView.titleLabel.numberOfLines = 0;//自动换行
[self.contentView addSubview:_textView];

}
return _textView;
}

- (UIImageView *)icon
{
if (_icon ==nil) {
_icon = [[UIImageView alloc] init];
[self.contentView addSubview:_icon];
}
return _icon;
}
//可以通过数据模型来设置数据和位置,也可以通过位置模型来设置数据和位置

//- (void)setMessageModel:(HMMessageModel *)messageModel
//{
//    _messageModel = messageModel;
//
//    self.time.text = self.messageModel.time;
//
//    [self.textView setTitle:self.messageModel.text forState:UIControlStateNormal];
//
//    if (self.messageModel.type == HMMessageModelGatsby) {
//        self.icon.image = [UIImage imageNamed:@"Gatsby"];
//    }else{
//        self.icon.image = [UIImage imageNamed:@"Jobs"];
//    }
//
//
//
//
//    //------------------frame----------------
//    CGFloat padding = 10;
//    //1. 时间
//    CGFloat timeX = 0;
//    CGFloat timeY = 0;
//    CGFloat timeW = bScreenWidth;
//    CGFloat timeH = bNormalH;
//
//    self.time.frame = CGRectMake(timeX, timeY, timeW, timeH);
//
//    //2.头像
//    CGFloat iconX;
//    CGFloat iconY = CGRectGetMaxY(self.time.frame);
//    CGFloat iconW = bIconW;
//    CGFloat iconH = bIconH;
//
//    if (self.messageModel.type == HMMessageModelGatsby) {//自己发的
//
//        iconX = bScreenWidth - iconW - padding;
//
//    }else{//别人发的
//        iconX = padding;
//    }
//
//    self.icon.frame =  CGRectMake(iconX, iconY, iconW, iconH);
//
//    //正文
//
//    CGFloat textX;
//    CGFloat textY = iconY;
//
//    CGSize textMaxSize = CGSizeMake(150, MAXFLOAT);
//    CGSize textRealSize = [ self.messageModel.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:bBtnFont} context:nil].size;
//
//    if (self.messageModel.type == HMMessageModelGatsby) {
//        textX = bScreenWidth - iconW - padding - textMaxSize.width;
//    }else{
//        textX = padding + iconW;
//    }
//
//    //    _textViewF = CGRectMake(textX, textY, <#CGFloat width#>, <#CGFloat height#>)
//    self.textView.frame = (CGRect){{textX,textY},textRealSize};
//
//    //4.cell高度
//
//    CGFloat iconMaxY = CGRectGetMaxY(self.icon.frame);
//    CGFloat textMaxY = CGRectGetMaxY(self.textView.frame);
//
//    HMMessageFrameModel *frameModel = [[HMMessageFrameModel alloc]init];
//    frameModel.cellH= MAX(iconMaxY, textMaxY);
//
//
//}

- (void)setFrameModel:(HMMessageFrameModel *)frameModel
{
_frameModel = frameModel;

HMMessageModel *messageModel = frameModel.message;

//1.时间
self.time.frame = frameModel.timeF;
self.time.text = messageModel.time;

//2.头像
self.icon.frame = frameModel.iconF;
if (messageModel.type == HMMessageModelGatsby) {
self.icon.image = [UIImage imageNamed:@"Gatsby"];
}else{
self.icon.image = [UIImage imageNamed:@"Jobs"];
}

//3.正文
self.textView.frame = frameModel.textViewF;
[self.textView setTitle:messageModel.text forState:UIControlStateNormal];
}

- (void)awakeFromNib {
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end


//
//  HMMessageFrameModel.h
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class HMMessageModel;
@interface HMMessageFrameModel : NSObject

//时间的frame
@property (nonatomic, assign,readonly)CGRect timeF;

//正文的frame
@property (nonatomic, assign,readonly)CGRect textViewF;

//图片
@property (nonatomic, assign,readonly)CGRect iconF;

//cell
@property (nonatomic, assign)CGFloat cellH;

//数据模型
@property (nonatomic, strong)HMMessageModel *message;

+ (NSArray *)messageFrame;

@end


//
//  HMMessageFrameModel.m
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMMessageFrameModel.h"
#import "HMMessageModel.h"
#import "Constant.h"

@implementation HMMessageFrameModel

- (void)setMessage:(HMMessageModel *)message
{
_message = message;

CGFloat padding = 10;
//1. 时间
CGFloat timeX = 0;
CGFloat timeY = 0;
CGFloat timeW = bScreenWidth;
CGFloat timeH = bNormalH;

_timeF = CGRectMake(timeX, timeY, timeW, timeH);

//2.头像
CGFloat iconX;
CGFloat iconY = CGRectGetMaxY(_timeF);
CGFloat iconW = bIconW;
CGFloat iconH = bIconH;

if (message.type == HMMessageModelGatsby) {//自己发的

iconX = bScreenWidth - iconW - padding;

}else{//别人发的
iconX = padding;
}

_iconF =  CGRectMake(iconX, iconY, iconW, iconH);
//3.正文

CGFloat textX;
CGFloat textY = iconY;

CGSize textMaxSize = CGSizeMake(150, MAXFLOAT);
CGSize textRealSize = [message.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:bBtnFont} context:nil].size;

if (message.type == HMMessageModelGatsby) {
textX = bScreenWidth - iconW - padding - textMaxSize.width;
}else{
textX = padding + iconW;
}

//    _textViewF = CGRectMake(textX, textY, <#CGFloat width#>, <#CGFloat height#>)
_textViewF = (CGRect){{textX,textY},textRealSize};

//4.cell高度

CGFloat iconMaxY = CGRectGetMaxY(_iconF);
CGFloat textMaxY = CGRectGetMaxY(_textViewF);

_cellH = MAX(iconMaxY, textMaxY);
}

+ (NSArray *)messageFrame
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];

NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
// 要添加statusFrame对象
HMMessageFrameModel *frameModel = [[HMMessageFrameModel alloc] init];

// 实例化一个新的Status模型
HMMessageModel *messageModel = [HMMessageModel messageWithDict:dict];

// 调用自己的setter方法,保存status数据模型,同时计算出所有控件的位置
frameModel.message = messageModel;

// 将statusFrame添加到数组
[arrayM addObject:frameModel];
}

return arrayM;
}
@end


//
//  HMMessageModel.h
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef enum {
HMMessageModelGatsby =0,
HMMessageModelJobs
}HMMessageModeltype;

@interface HMMessageModel : NSObject
@property(nonatomic,copy) NSString *text;
@property(nonatomic,copy) NSString *time;
@property(nonatomic,assign) HMMessageModeltype type;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)messageWithDict:(NSDictionary *)dict;

//+ (NSArray *)Messages;

@end


//
//  HMMessageModel.m
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMMessageModel.h"

@implementation HMMessageModel
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}

+ (instancetype)messageWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}

//+ (NSArray *)Messages
//{
//    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];
//    NSMutableArray *arrayM = [NSMutableArray array];
//    for (NSDictionary *dict in array) {
//        [arrayM addObject:[self messageWithDict:dict]];
//    }
//
//    return arrayM;
//}

@end


//
//  ViewController.h
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


//
//  ViewController.m
//  QQ聊天
//
//  Created by YaguangZhu on 15/8/27.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"
#import "HMMessageModel.h"
#import "HMMessageCell.h"
#import "HMMessageFrameModel.h"

@interface ViewController ()<UITabBarControllerDelegate,UITableViewDataSource>
//@property(nonatomic,strong) NSArray *messages;
@property(nonatomic,strong) NSArray *messagesFrame;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

//-(NSArray *)messages
//{
//    if (_messages == nil) {
//        _messages = [HMMessageModel messages];
//    }
//    return _messages;
//}
- (NSArray *)messagesFrame
{
if (_messagesFrame == nil) {
_messagesFrame = [HMMessageFrameModel messageFrame];
}
return _messagesFrame;
}

//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.messagesFrame.count;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *ID =@"cell";
//
//    HMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//
//    if (cell ==nil) {
//        cell = [[HMMessageCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
//
//    }

HMMessageCell *cell = [HMMessageCell messageCellWithTableView:tableView];

// cell.messageModel =self.messages[indexPath.row];

HMMessageFrameModel *model = self.messagesFrame[indexPath.row];

cell.frameModel =model;

return cell;

}

#pragma mark - 代理方法
/** 计算单元格行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
计算行高的方法,会在加载表格数据时,有多少行计算多少次 contentSize

问题:此方法执行的时候,cell还没有被实例化!
但是:行高计算是在实例化cell时,通过设置status属性,计算的=>有了status模型,就可以知道行高!

问题:如何在cell实例化之前,获得行高?
解决方法:通过status可以计算得到行高!=》再建立一个模型,专门计算所有控件的位置
*/

//    HMMessageFrameModel *messageFrame1 = [[HMMessageFrameModel alloc] init];
//    messageFrame1.message =self.messagesFrame[indexPath.row];
//    return messageFrame1.cellH;

HMMessageFrameModel *messageFrame1 = self.messagesFrame[indexPath.row];
return messageFrame1.cellH;

//return 200;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: