IOS第九天(1:QQ聊天界面frame模型)

news/2024/7/24 2:30:44

 

///  控制层

#import "HMViewController.h"
#import "HMMessageModel.h"
#import "HMMessageCell.h"
#import "HMMessageFrameModel.h"
@interface HMViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray *messages;

@end

@implementation HMViewController

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

- (NSMutableArray *)messages
{
    if (_messages == nil) {
        
        NSArray * array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];
        
        NSMutableArray *messageArr = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            HMMessageModel *messga = [HMMessageModel messageWithDict:dict];
            
            HMMessageFrameModel *fm = [[HMMessageFrameModel alloc]init];
            fm.message = messga;
            
            [messageArr addObject:fm];
        }
        
        _messages = messageArr;        
    }
    
    return _messages;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMMessageFrameModel *model = self.messages[indexPath.row];
    return model.cellH;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMMessageCell *cell = [HMMessageCell messageCellWithTableView:tableView];
    HMMessageFrameModel *model = self.messages[indexPath.row];
    
    cell.frameMessage = model;
    
    return cell;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface HMViewController : UIViewController

@end

 ///   model.h

#import <Foundation/Foundation.h>
typedef enum {
    HMMessageModelGatsby = 0,//Gatsby
    HMMessageModelJobs//Jobs
}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;

@end

  *****model.m

#import "HMMessageModel.h"

@implementation HMMessageModel

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

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

@end

******modelframe.h

#import <Foundation/Foundation.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,readonly)CGFloat cellH;

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

******modelframe.m

#import "HMMessageFrameModel.h"
#import "Constant.h"
#import "HMMessageModel.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);
    
    
}

@end

****cell.h

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

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

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

@end

****cell.m

#import "HMMessageCell.h"
#import "HMMessageFrameModel.h"
#import "HMMessageModel.h"
#import "Constant.h"
@interface HMMessageCell()
//时间
@property (nonatomic, weak)UILabel *time;
//正文
@property (nonatomic, weak)UIButton *textView;
//用户头像
@property (nonatomic, weak)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;
}
// 初始化控件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       //1.时间
        UILabel *time = [[UILabel alloc]init];
        time.textAlignment = NSTextAlignmentCenter;
        time.font = [UIFont systemFontOfSize:13.0f];
        [self.contentView addSubview:time];
        self.time = time;
        
        //1.正文
        UIButton *textView = [[UIButton alloc]init];
        textView.backgroundColor = [UIColor grayColor];
        textView.titleLabel.font = bBtnFont;
        textView.titleLabel.numberOfLines = 0;//自动换行
        [self.contentView addSubview:textView];
        self.textView = textView;
        
        //1.头像
        UIImageView *icon = [[UIImageView alloc]init];
        [self.contentView addSubview:icon];
        self.icon = icon;
        
    }
    return self;
}
// 设置位置和值
- (void)setFrameMessage:(HMMessageFrameModel *)frameMessage
{
    _frameMessage = frameMessage;
    
    HMMessageModel *model = frameMessage.message;
    
    //1.时间
    self.time.frame = frameMessage.timeF;
    self.time.text = model.time;
    
    //2.头像
    self.icon.frame = frameMessage.iconF;
    if (model.type == HMMessageModelGatsby) {
        self.icon.image = [UIImage imageNamed:@"Gatsby"];
    }else{
        self.icon.image = [UIImage imageNamed:@"Jobs"];
    }
    
    //3.正文
    self.textView.frame = frameMessage.textViewF;
    [self.textView setTitle:model.text forState:UIControlStateNormal];
    
}
@end

 

转载于:https://www.cnblogs.com/ios-g/p/4731941.html


http://www.niftyadmin.cn/n/1095616.html

相关文章

C/C++学习的50个经典网站

C/C是最主要的编程语言。这里列出了50名优秀网站和网页清单&#xff0c;这些网站提供c/c源代码。这份清单提供了源代码的链接以及它们的小说明。我已尽力包括最佳的C/C源代码的网站。这不是一个完整的清单&#xff0c;您有建议可以联系我&#xff0c;我将欢迎您的建议&#xff…

win10 系统如何设置环境变量

第一种&#xff1a; 第二种&#xff1a; 在这里输入环境变量 之后按照上述步骤修改path即可

为什么数据库要有主键

主键&#xff1a; 概念 主关键字&#xff08;主键&#xff0c;primary key&#xff09;是被挑选出来&#xff0c;作表的行的惟一标识的候选关键字。一个表只有一个主关键字。主关键字又可以称为主键。 主键可以由一个字段&#xff0c;也可以由多个字段组成&#xff0c;分别成…

关于MySQL count(distinct) 逻辑的一个bug【转】

本文来自&#xff1a;http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误&#xff0c;实际结果存在值&#xff0c;但是用count(distinct)统计后返回的是0。将问题简化后复现如下&#xff0c;影响已知的所有版本。 这里的 set…

数据结构入门进阶必学-图的遍历

图介绍图的抽象数据结构边列表邻接列表邻接图领结矩阵图的遍历深度优先遍历思想深度优先遍历算法步骤图的广度优先搜索(Broad First Search)广度优先遍历算法步骤介绍 在计算机科学中&#xff0c;一个图就是一些顶点的集合&#xff0c;这些顶点通过一系列边结对&#xff08;连…

如何在pycharm中配置anaconda环境

最后配置anaconda环境即可。

能除甲醛的一些植物

除甲醛植物&#xff1a;白掌&#xff1a;抑制人体呼出的废气,如氨气和丙酮.同时它也可以过滤空气中的苯、三氯乙烯和甲醛。吊兰&#xff1a;能吸收空气中95%的一氧化碳和85%的甲醛。芦荟&#xff1a;一盆芦荟相当于九台生物空气清洁器&#xff0c;一盆芦荟可消除一平方米空气中…

题目1019:简单计算器

题目描述&#xff1a;读入一个只包含 , -, *, / 的非负整数计算表达式&#xff0c;计算该表达式的值。输入&#xff1a;测试输入包含若干测试用例&#xff0c;每个测试用例占一行&#xff0c;每行不超过200个字符&#xff0c;整数和运算符之间用一个空格分隔。没有非法表达式。…