iOS开发-QQ聊天布局实现(聊天机器人应用)

今天和同学们讲一下QQ的聊天布局,以及如何实现一个聊天机器人,很多细节以及分类的处理!那么老规矩废话不多说上代码!

 

#pragma mark - 分类

//

//  UIImage+ZZ.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface UIImage (ZZ)

+ (UIImage *)resizableImage:(NSString *)name;

@end

 

//

//  UIImage+ZZ.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "UIImage+ZZ.h"

 

@implementation UIImage (ZZ)

/**

 *  返回一张可以随意拉伸不变形的图片

 *

 *  @param name 图片名字

 */

+ (UIImage *)resizableImage:(NSString *)name

{

    UIImage *normal = [UIImage imageNamed:name];

    CGFloat w = normal.size.width *0.5;

    CGFloat h = normal.size.height *0.5;

    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];

}

@end

 

//

//  NSString+ZZ.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

 

@interface NSString (ZZ)

/**

 *  返回字符串所占用的尺寸

 *

 *  @param font    字体

 *  @param maxSize 最大尺寸

 */

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize;

@end

 

//

//  NSString+ZZ.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "NSString+ZZ.h"

 

@implementation NSString (ZZ)

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize

{

    NSDictionary *attrs = @{NSFontAttributeName : font};

    

    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

    

}

@end

 

#pragma mark - 模型model

 

//

//  ZZMessage.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import <Foundation/Foundation.h>

typedef enum {

    ZZMessageTypeMe = 0,//自己发的

    ZZMessageTypeOther   // 别人发的

} ZZMessageType;

#pragma mark - 那么我们先来根据plist解决模型的问题首先提供一个最基本的模型

@interface ZZMessage : NSObject

/**

 *  聊天内容

 */

@property (nonatomic,copy)NSString *text;

/**

 *  发送时间

 */

@property (nonatomic,copy)NSString *time;

/**

 *  信息的类型

 */

@property (nonatomic,assign)ZZMessageType type;

 

/**

 *  是否隐藏时间

 */

@property (nonatomic,assign)BOOL hideTime;

 

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

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

@end

 

//

//  ZZMessage.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "ZZMessage.h"

 

@implementation ZZMessage

+ (instancetype)messageWithDict:(NSDictionary *)dict

{

    return [[self alloc]initWithDict:dict];

}

 

- (instancetype)initWithDict:(NSDictionary *)dict

{

    if (self = [super init]) {//直接通过kvc赋值

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

@end

 

//

//  ZZMessageFrame.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

#pragma mark - 再提供一个ZZMessageFrame的模型内部承载ZZMessage模型

// 正文的字体

#define ZZTextFont [UIFont systemFontOfSize:15]

 

// 正文的内边距

#define ZZTextPadding 20

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

 

@class ZZMessage;

 

@interface ZZMessageFrame : NSObject

/**

 *  头像的frame

 */

@property (nonatomic,assign,readonly)CGRect iconF;

/**

 *  时间的frame

 */

@property (nonatomic,assign,readonly)CGRect timeF;

/**

 *  正文的frame

 */

@property (nonatomic,assign,readonly)CGRect textF;

/**

 *  cell的高度

 */

@property (nonatomic,assign,readonly)CGFloat cellHeight;

 

/**

 *  数据模型

 */

@property (nonatomic,strong)ZZMessage *message;

@end

 

//

//  ZZMessageFrame.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "ZZMessageFrame.h"

#import "ZZMessage.h"

#import "NSString+ZZ.h"

 

@implementation ZZMessageFrame

- (void)setMessage:(ZZMessage *)message

{

    _message =  message;

    // 间距

    CGFloat padding = 10;

    // 屏幕的宽度

    CGFloat screenW =  [UIScreen mainScreen].bounds.size.width;

    

    // 1.时间

    if (message.hideTime == NO) {//显示时间

        CGFloat timeX = 0;

        CGFloat timeY = 0;

        CGFloat timeW = screenW;

        CGFloat timeH = 40;

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

    }

    

    // 2.头像

    CGFloat iconY =  CGRectGetMaxY(_timeF) + padding;

    CGFloat iconW =  40;

    CGFloat iconH =  40;

    CGFloat iconX;

    if (message.type == ZZMessageTypeOther) {//别人发的

        iconX = padding;

    } else {//自己的发的

        iconX = screenW - padding - iconW;

    }

    _iconF = CGRectMake(iconX, iconY, iconW, iconH);

    

    // 3.正文

    CGFloat textY =  iconY;

    // 文字计算的最大尺寸

    CGSize textMaxSize = CGSizeMake(200,MAXFLOAT);

    // 文字计算出来的真实尺寸(按钮内部label的尺寸)

    CGSize textRealSize =  [message.text sizeWithFont:ZZTextFont maxSize:textMaxSize];

    // 按钮最终的真实尺寸

    CGSize textBtnSize = CGSizeMake(textRealSize.width +ZZTextPadding *2, textRealSize.height +ZZTextPadding *2);

    CGFloat textX;

    if (message.type == ZZMessageTypeOther) {//别人发的

        textX = CGRectGetMaxX(_iconF) + padding;

    } else {//自己的发的

        textX = iconX - padding - textBtnSize.width;

    }

    //    _textF =  CGRectMake(textX, textY, textSize.width + 40, textSize.height + 40);

    _textF = (CGRect){{textX, textY}, textBtnSize};

    

    // 4.cell的高度

    CGFloat textMaxY =  CGRectGetMaxY(_textF);

    CGFloat iconMaxY =  CGRectGetMaxY(_iconF);

    _cellHeight = MAX(textMaxY, iconMaxY) + padding;

}

@end

 

#pragma mark - 自定义cell & view

//

//  ZZInputView.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@protocol ZZInputViewDelegate <NSObject>

@optional

 

- (void)inputViewDidSendMsg:(NSString *)msgStr;

 

@end

 

@interface ZZInputView : UIView

+ (instancetype)inputView;

 

@property (nonatomic, weak) id <ZZInputViewDelegate> delegate;

@end

 

//

//  ZZInputView.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "ZZInputView.h"

#import "UIImage+ZZ.h"

 

@interface ZZInputView()<UITextFieldDelegate>

@property (nonatomic, weak) UIImageView *bgImg;

@property (nonatomic, weak) UIButton *voiceBtn;

@property (nonatomic, weak) UITextField *msgTf;

@property (nonatomic, weak) UIButton *smileBtn;

@property (nonatomic, weak) UIButton *addBtn;

@end

 

@implementation ZZInputView

+ (instancetype)inputView

{

    return [[self alloc] init];

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        [self setUpSubViews];

    }

    

    return self;

}

 

- (instancetype)initWithCoder:(NSCoder *)decoder

{

    if (self = [super initWithCoder:decoder]) {

        [self setUpSubViews];

    }

    

    return self;

}

 

- (void)setUpSubViews

{

    UIImageView *bgImg = [[UIImageView alloc] init];

    bgImg.image = [UIImage resizableImage:@"chat_bottom_bg"];

    [self addSubview:bgImg];

    self.bgImg = bgImg;

    

    UIButton *voiceBtn = [[UIButton alloc] init];

    [voiceBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_voice_nor"] forState:UIControlStateNormal];

    [voiceBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_voice_press"] forState:UIControlStateHighlighted];

    [self addSubview:voiceBtn];

    self.voiceBtn = voiceBtn;

    

    UITextField *msgTf = [[UITextField alloc] init];

    msgTf.delegate = self;

    msgTf.borderStyle = UITextBorderStyleNone;

    msgTf.background = [UIImage imageNamed:@"chat_bottom_textfield"];

    msgTf.adjustsFontSizeToFitWidth = YES;

    msgTf.returnKeyType = UIReturnKeySend;

    msgTf.font = [UIFont systemFontOfSize:14.0f];

    [self addSubview:msgTf];

    self.msgTf = msgTf;

    

    UIButton *smileBtn = [[UIButton alloc] init];

    [smileBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_smile_nor"] forState:UIControlStateNormal];

    [smileBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_smile_press"] forState:UIControlStateHighlighted];

    [self addSubview:smileBtn];

    self.smileBtn = smileBtn;

    

    UIButton *addBtn = [[UIButton alloc] init];

    [addBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_up_nor"] forState:UIControlStateNormal];

    [addBtn setBackgroundImage:[UIImage imageNamed:@"chat_bottom_up_press"] forState:UIControlStateHighlighted];

    [self addSubview:addBtn];

    self.addBtn = addBtn;

}

 

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    CGFloat bgImgX = 0;

    CGFloat bgImgY = 0;

    CGFloat bgImgW = self.frame.size.width;

    CGFloat bgImgH = 44;

    self.bgImg.frame = CGRectMake(bgImgX, bgImgY, bgImgW, bgImgH);

    

    CGFloat voiceBtnX = 5;

    CGFloat voiceBtnY = bgImgY;

    CGFloat voiceBtnW = bgImgH;

    CGFloat voiceBtnH = bgImgH;

    self.voiceBtn.frame = CGRectMake(voiceBtnX, voiceBtnY, voiceBtnW, voiceBtnH);

    

    CGFloat msgTfX = voiceBtnX + bgImgH;

    CGFloat msgTfY = 1;

    CGFloat msgTfW = self.frame.size.width - 3 * (bgImgH + voiceBtnX) - voiceBtnX;

    CGFloat msgTfH = bgImgH - msgTfY;

    self.msgTf.frame = CGRectMake(msgTfX, msgTfY, msgTfW, msgTfH);

    

    CGFloat smileBtnX = self.frame.size.width - 2 * (bgImgH + voiceBtnX);

    CGFloat smileBtnY = bgImgY;

    CGFloat smileBtnW = bgImgH;

    CGFloat smileBtnH = bgImgH;

    self.smileBtn.frame = CGRectMake(smileBtnX, smileBtnY, smileBtnW, smileBtnH);

    

    CGFloat addBtnX = self.frame.size.width - (bgImgH + voiceBtnX);

    CGFloat addBtnY = bgImgY;

    CGFloat addBtnW = bgImgH;

    CGFloat addBtnH = bgImgH;

    self.addBtn.frame = CGRectMake(addBtnX, addBtnY, addBtnW, addBtnH);

}

#pragma mark - 文本框代理

/**

 *  点击了return按钮(键盘最右下角的按钮)就会调用

 */

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    // 1.通知代理

    if ([self.delegate respondsToSelector:@selector(inputViewDidSendMsg:)]) {

        [self.delegate inputViewDidSendMsg:textField.text];

    }

    

    // 2.清空文字

    textField.text = nil;

    

    // 返回YES即可

    return YES;

}

@end

 

//

//  ZZMessageCell.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#pragma mark - 这里写出自定义cell

#import <UIKit/UIKit.h>

 

@class ZZMessageFrame;

@interface ZZMessageCell : UITableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView;

 

@property (nonatomic,strong)ZZMessageFrame *messageFrame;

@end

 

//

//  ZZMessageCell.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

#import "ZZMessageCell.h"

#import "ZZMessageFrame.h"

#import "ZZMessage.h"

#import "UIImage+ZZ.h"

 

@interface ZZMessageCell()

/**

 *  时间

 */

@property (nonatomic,weak) UILabel *timeView;

/**

 *  头像

 */

@property (nonatomic,weak) UIImageView *iconView;

/**

 *  正文

 */

@property (nonatomic,weak) UIButton *textView;

@end

 

@implementation ZZMessageCell

+ (instancetype)cellWithTableView:(UITableView *)tableView

{

    static NSString *ID =@"message";

    ZZMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell ==nil) {

        cell = [[ZZMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    return cell;

}

 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // 子控件的创建和初始化

        // 1.时间

        UILabel *timeView = [[UILabel alloc]init];

        timeView.textAlignment = NSTextAlignmentCenter;

        timeView.textColor = [UIColor grayColor];

        timeView.font = [UIFont systemFontOfSize:13];

        [self.contentView addSubview:timeView];

        self.timeView = timeView;

        

        // 2.头像

        UIImageView *iconView = [[UIImageView alloc]init];

        [self.contentView addSubview:iconView];

        self.iconView = iconView;

        

        // 3.正文

        UIButton *textView = [[UIButton alloc]init];

        textView.titleLabel.numberOfLines = 0;//自动换行

        textView.titleLabel.font = ZZTextFont;

        textView.contentEdgeInsets = UIEdgeInsetsMake(ZZTextPadding, ZZTextPadding, ZZTextPadding, ZZTextPadding);

        [textView setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

        [self.contentView addSubview:textView];

        self.textView = textView;

        

        // 4.设置cell的背景色

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

 

- (void)setMessageFrame:(ZZMessageFrame *)messageFrame

{

    _messageFrame = messageFrame;

    

    ZZMessage *message = messageFrame.message;

    

    // 1.时间

    self.timeView.text = message.time;

    self.timeView.frame = messageFrame.timeF;

    

    // 2.头像

    NSString *icon = (message.type == ZZMessageTypeMe) ?@"me" :@"other";

    self.iconView.image = [UIImage imageNamed:icon];

    self.iconView.frame = messageFrame.iconF;

    

    // 3.正文

    [self.textView setTitle:message.text forState:UIControlStateNormal];

    self.textView.frame = messageFrame.textF;

    

    // 4.正文的背景

    if (message.type == ZZMessageTypeMe) {//自己发的,蓝色

        [self.textView setBackgroundImage:[UIImage resizableImage:@"chat_send_nor"] forState:UIControlStateNormal];

    } else {//别人发的,白色

        [self.textView setBackgroundImage:[UIImage resizableImage:@"chat_recive_nor"] forState:UIControlStateNormal];

    }

}

 

@end

 

#pragma mark - controller

 

 

//

//  ZZMessageController.h

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

 

#import <UIKit/UIKit.h>

 

@interface ZZMessageController : UIViewController

 

@end

 

 

 

 

//

//  ZZMessageController.m

//  03-QQ聊天布局

//

//  Created by 周昭 on 2017/3/14.

//  Copyright © 2017年 ZZ. All rights reserved.

//

 

 

#import "ZZMessageController.h"

#import "ZZMessage.h"

#import "ZZMessageFrame.h"

#import "ZZMessageCell.h"

#import "ZZInputView.h"

 

@interface ZZMessageController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,ZZInputViewDelegate>

@property (nonatomic,strong) NSMutableArray *messageFrames;

@property (nonatomic,strong) NSDictionary *autoreply;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, weak) UITextField *inputView;

@end

 

@implementation ZZMessageController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 1.表格的设置

    [self setUpTableView];

    

    // 2.监听键盘的通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

    

    // 3.底部的输入view

    [self setUpInputView];

}

 

#pragma mark - setUp 初始化

/**

 *  表格的设置

 */

- (void)setUpTableView

{

    UITableView *tableView = [[UITableView alloc] init];

    tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44);

    tableView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0];

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //去除分割线

    tableView.allowsSelection = NO; //不允许选中

    tableView.delegate = self;

    tableView.dataSource = self;

    [self.view addSubview:tableView];

    self.tableView = tableView;

}

 

/**

 *  底部的输入view

 */

- (void)setUpInputView

{

    ZZInputView *inputView = [ZZInputView inputView];

    inputView.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);

    inputView.delegate = self;

    [self.view addSubview:inputView];

}

 

#pragma mark - ZZInputViewDelegate 方法

- (void)inputViewDidSendMsg:(NSString *)msgStr

{

    // 1.自己发一条消息

    [self addMessage:msgStr type:ZZMessageTypeMe];

    

    // 2.自动回复一条消息

    NSString *reply = [self replayWithText:msgStr];

    [self addMessage:reply type:ZZMessageTypeOther];

}

 

/**

 *  发送一条消息

 */

- (void)addMessage:(NSString *)text type:(ZZMessageType)type

{

    // 1.数据模型

    ZZMessage *msg = [[ZZMessage alloc] init];

    msg.type = type;

    msg.text = text;

    // 设置数据模型的时间

    NSDate *now = [NSDate date];

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];

    fmt.dateFormat =@"HH:mm";

    // NSDate  --->  NSString

    // NSString ---> NSDate

    //    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    //  2014-08-09 15:45:56

    // 09/08/2014  15:45:56

    msg.time = [fmt stringFromDate:now];

    

    // 看是否需要隐藏时间

    ZZMessageFrame *lastMf = [self.messageFrames lastObject];

    ZZMessage *lastMsg = lastMf.message;

    msg.hideTime = [msg.time isEqualToString:lastMsg.time];

    

    // 2.frame模型

    ZZMessageFrame *mf = [[ZZMessageFrame alloc] init];

    mf.message = msg;

    [self.messageFrames addObject:mf];

    

    // 3.刷新表格

    [self.tableView reloadData];

    

    // 4.自动滚动表格到最后一行

    NSIndexPath *lastPath = [NSIndexPath indexPathForRow:self.messageFrames.count - 1 inSection:0];

    [self.tableView scrollToRowAtIndexPath:lastPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}

 

/**

 *  根据自己发的内容取得自动回复的内容

 *

 *  @param text 自己发的内容

 */

- (NSString *)replayWithText:(NSString *)text

{

//    for (int i = 0; i < text.length; i++) {

//        NSString *word = [text substringWithRange:NSMakeRange(i,1)];

//        

//        if (self.autoreply[word]) return self.autoreply[word];

//    }

    if ([text isEqualToString:@"1"]) {

        return @"110";

    } else if ([text isEqualToString:@"2"]) {

        return @"120";

    } else if ([text isEqualToString:@"3"]) {

        return @"911";

    } else if ([text isEqualToString:@"4"]) {

        return @"119";

    } else

        return@"滚蛋";

}

 

/**

 *  当键盘改变了frame(位置和尺寸)的时候调用

 */

- (void)keyboardWillChangeFrame:(NSNotification *)note

{

    // 设置窗口的颜色

    self.view.window.backgroundColor = self.tableView.backgroundColor;

    

    // 0.取出键盘动画的时间

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];

    

    // 1.取得键盘最后的frame

    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    // 2.计算控制器的view需要平移的距离

    CGFloat transformY = keyboardFrame.origin.y -self.view.frame.size.height;

    

    // 3.执行动画

    [UIView animateWithDuration:duration animations:^{

        

        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);

    }];

}

 

/*

 UIKeyboardAnimationCurveUserInfoKey = 7;  // 动画的执行节奏(速度)

 UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 键盘弹出\隐藏动画所需要的时间

 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";

 UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";

 UIKeyboardFrameChangedByUserInteraction = 0;

 

 // 键盘弹出

 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";//键盘刚出来那一刻的frame

 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; // 键盘显示完毕后的frame

 

 // 键盘隐藏

 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";

 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";

 */

- (BOOL)prefersStatusBarHidden

{

    return YES;

}

 

- (NSDictionary *)autoreply

{

    if (_autoreply ==nil) {

        _autoreply = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"autoreply.plist" ofType:nil]];

    }

    return _autoreply;

}

 

- (NSMutableArray *)messageFrames

{

    if (_messageFrames ==nil) {

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];

        

        NSMutableArray *mfArray = [NSMutableArray array];

        

        for (NSDictionary *dict in dictArray) {

            // 消息模型

            ZZMessage *msg = [ZZMessage messageWithDict:dict];

            

            // 取出上一个模型

            ZZMessageFrame *lastMf = [mfArray lastObject];

            ZZMessage *lastMsg = lastMf.message;

            

            // 判断两个消息的时间是否一致

            msg.hideTime = [msg.time isEqualToString:lastMsg.time];

            

            // frame模型

            ZZMessageFrame *mf = [[ZZMessageFrame alloc] init];

            mf.message = msg;

            

            // 添加模型

            [mfArray addObject:mf];

        }

        

        _messageFrames = mfArray;

    }

    return _messageFrames;

}

 

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.messageFrames.count;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 1.创建cell

    ZZMessageCell *cell = [ZZMessageCell cellWithTableView:tableView];

    

    // 2.给cell传递模型

    cell.messageFrame = self.messageFrames[indexPath.row];

    

    // 3.返回cell

    return cell;

}

 

#pragma mark - 代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    ZZMessageFrame *mf = self.messageFrames[indexPath.row];

    return mf.cellHeight;

}

 

/**

 *  当开始拖拽表格的时候就会调用

 */

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    // 退出键盘

    [self.view endEditing:YES];

}

 

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值