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

消除重复计算的缺点,既是将有两个模型,一个数据模型,一个位置模型。位置模型里面包含了数据模型,将计算位置和数据都一起做了。

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 [email protected]"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;
}
时间: 2024-08-11 05:33:11

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

ios学习(界面传值的方法)

ios(学习)界面传值的方法 block: 实现界面传值的方法1.block: 实现界面传值,都是从第二个界面向第一个界面传值:第一种block 首先).在第二个界面secondViewController声明set方法 声明block @property (nonatomic,copy) void(^change)(UIColor *color); 其次).在.m文件实现 实现block的方法 _callback([UIColor redColor]);//注意这里之所以是_callback的

iOS创建界面方法的讨论

以前在入门的时候,找的入门书籍上编写的 demo 都是基于 Storyboards 拖界面的.后来接触公司项目,发现界面都是用纯代码去写复杂的 autoLayout 的.再然后,领导给我发了个 Masonry 库去看,依然是手写代码布局界面,但效率高了不少.工作一段时间,看了很多博客,也看了一些书,发现用纯代码写界面的很少,于是就在 Google 上搜 Storyboards 有什么好处,最后发现了一篇非常好的文章.在此提炼文章的一些观点,同时表达一下自己的观点. 文章链接:iOS User I

iOS 实现QQ界面

应师傅要求编写个QQ界面来不吝赐教下我的代码问题. 编写个QQ界面.有三个组,每一个组有人.并显示在线不在线. 先看一下效果图 这里省了事由于我的图片仅仅用了一张.假设要依据人的不同设置,仅仅要在cell里面改一下即可了: 主要是实现点击上面分组展开内容,再点击收回去. 废话不多说,上菜: 我们先来构造数据: NSArray * InitArray [email protected][ @{ @"Name":@"朋友", @"Group":@&q

xode5.1.1设置IOS欢迎界面的方法

先准备3张不同尺寸的欢迎图,文件名分别为: Default.png  iPhone 320X480分辨率屏幕默认启动图片. [email protected] iPhone 640X960分辨率屏幕默认启动图片. [email protected]   iPhone 640X1136分辨率屏幕默认启动图片. 把这3张图放在工程的Supporting Files文件夹下 打开工程属性中的"Launch Images" 把这3张图片,拖到对应的位置就OK了. xode5.1.1设置IOS欢

iOS仿QQ界面

iOS仿QQ界面 仿制QQ5.0的界面,可以切换主题,并且有左右滑动特效. 下载地址:http://www.dwz.cn/z08ik 源码运行截图

iOS手动控制界面旋转

条条大道通罗马,解决同一个问题的手段也是多种多样的.对于<iOS 6及以上控制个别视图旋转案例>中提到的案例,我们是利用系统自带的旋转机制来解决问题的.同样地,我们也可以自己coding解决问题,且最终效果同系统的旋转动画效果是一模一样的.废话不多说,下面来大概讲解一下. 手动控制界面旋转的核心思路就是利用UIView的transform属性,旋转App的根视图.何为根视图?如果你的App的window.rootViewController是UINavigationController,那么根

ios图片拉伸两种方法

ios图片拉伸两种方法 UIImage *image = [UIImage imageNamed:@"qq"]; 第一种: // 左端盖宽度 NSInteger leftCapWidth = image.size.width * 0.5f; // 顶端盖高度 NSInteger topCapHeight = image.size.height * 0.5f; // 重新赋值 image = [image stretchableImageWithLeftCapWidth:leftCapW

多种多样的App主界面Tab实现方法

看了一下App主界面Tab实现方法,总结一下: 再看看实现的效果图:                            第一种:ViewPager实现Tab: 思路:1.布局方面实现顶层和底层布局,中间是ViewPager实现的.中间是四个布局实现的,List<View>                 2.实现:setOnPageChangeListener内部类                3.需要一个适配器:PagerAdapter 源代码如下: <span style=&q

IOS开发中重写init方法使用需谨慎

IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图控制器中,然后把搜索结果呈现在一个TableView上,但是我在调试时,每次输入完然后点击搜索按钮时,弹出结果列表总是空的,我需要返回到首页再点击一次搜索才会出现结果,而且我在首页更改搜索关键字之后,点击搜索,结果还是上次的搜索结果,必须返回点击一次才会出现这次的搜索结果. 经过大神指点,原来这个问