还真是失败,搞了两天遇到了布局cell高度总是出差的问题,cell height不是高很多很多,就是就是矮到没有的情况。。。。糟糕透顶待解救~

聊天布局

//
//  XMGChatingViewController.m
//  07-聊天布局
#import "XMGChatingViewController.h"
#import "XMGMessage.h"
#import "XMGMessageCell.h"@interface XMGChatingViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSArray *messages;
@end@implementation XMGChatingViewController- (NSArray *)messages
{if (_messages == nil) {// 加载plist中的字典数组NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];// 字典数组 -> 模型数组NSMutableArray *messageArray = [NSMutableArray array];for (NSDictionary *dict in dictArray) {XMGMessage *message = [XMGMessage messageWithDict:dict];[messageArray addObject:message];}_messages = messageArray;}return _messages;
}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.

}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return self.messages.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"message"];cell.message = self.messages[indexPath.row];return cell;
}#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 200;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{XMGMessage *message = self.messages[indexPath.row];return message.cellHeight;
}
@end

//  XMGMessageCell.h
//  07-聊天布局
#import <UIKit/UIKit.h>
@class XMGMessage;@interface XMGMessageCell : UITableViewCell
@property (nonatomic, strong) XMGMessage *message;
@end

//  XMGMessageCell.m
//  07-聊天布局
#import "XMGMessageCell.h"
#import "XMGMessage.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"@interface XMGMessageCell()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *textButton;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UIButton *otherTextButton;
@property (weak, nonatomic) IBOutlet UIImageView *otherIconView;
@end@implementation XMGMessageCell- (void)awakeFromNib
{self.textButton.titleLabel.numberOfLines = 0;self.otherTextButton.titleLabel.numberOfLines = 0;
}- (void)setMessage:(XMGMessage *)message
{_message = message;self.timeLabel.text = message.time;if (message.type == XMGMessageTypeMe) { // 右边
        [self settingShowTextButton:self.textButton showIconView:self.iconView hideTextButton:self.otherTextButton hideIconView:self.otherIconView];} else { // 左边
        [self settingShowTextButton:self.otherTextButton showIconView:self.otherIconView hideTextButton:self.textButton hideIconView:self.iconView];}
}/*** 处理左右按钮、头像*/
- (void)settingShowTextButton:(UIButton *)showTextButton showIconView:(UIImageView *)showIconView hideTextButton:(UIButton *)hideTextButton hideIconView:(UIImageView *)hideIconView
{hideTextButton.hidden = YES;hideIconView.hidden = YES;showTextButton.hidden = NO;showIconView.hidden = NO;// 设置按钮的文字
    [showTextButton setTitle:self.message.text forState:UIControlStateNormal];// 强制更新
    [showTextButton layoutIfNeeded];// 设置按钮的高度就是titleLabel的高度[showTextButton updateConstraints:^(MASConstraintMaker *make) {CGFloat buttonH = showTextButton.titleLabel.frame.size.height;make.height.equalTo(buttonH);}];// 强制更新
    [showTextButton layoutIfNeeded];// 计算当前cell的高度CGFloat buttonMaxY = CGRectGetMaxY(showTextButton.frame);CGFloat iconMaxY = CGRectGetMaxY(showIconView.frame);self.message.cellHeight = MAX(buttonMaxY, iconMaxY) + 10;
}@end

//  XMGMessage.h
//  07-聊天布局
#import <UIKit/UIKit.h>typedef enum {XMGMessageTypeMe = 0,XMGMessageTypeOther = 1
} XMGMessageType;@interface XMGMessage : NSObject
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *time;
@property (nonatomic, assign) XMGMessageType type;@property (nonatomic, assign) CGFloat cellHeight;+ (instancetype)messageWithDict:(NSDictionary *)dict;
@end

//  XMGMessage.m
//  07-聊天布局
#import "XMGMessage.h"@implementation XMGMessage+ (instancetype)messageWithDict:(NSDictionary *)dict
{XMGMessage *message = [[self alloc] init];[message setValuesForKeysWithDictionary:dict];return message;
}
@end

转载于:https://www.cnblogs.com/laugh/p/6474766.html

源码-0205-02--聊天布局相关推荐

  1. 直播app源码,matlab 色彩布局描述符

    直播app源码,matlab 色彩布局描述符 %color layout descriptor (CLD) %im = imread('cs.jpg'); function result = CLD( ...

  2. 一对一直播源码、一对一聊天app源码前端后台功能说明

    一对一直播源码前端 后台功能说明 随着直播平台的不断更新换代,一对一直播越来越受关注,其兼容性.强锁粉.高变现.财务明晰等特性,备受运营商的青睐.软件功能上的变化离不开底层源码构架的开发,下面就一对一 ...

  3. Android 源码 图形系统之请求布局

    在<Android 源码 图形系统之窗口添加>一节中遗留了 ViewRootImpl 类 setView 方法中调用 requestLayout() 函数分析.现在继续分析其流程.分析之前 ...

  4. 直播平台源码关于服务器聊天实现示列

    直播平台源码中聊天服务器是通过socket.io实现的,在功能设计中,很多功能需要通过接口操作后广播到客户端实现消息通知,单纯使用php是无法实现的,需要在php处理后将消息通过socket.io通知 ...

  5. Android4.2.2 Gallery2源码分析(1)——从布局入手

    使用的是现有的项目的源码进行分析,它在Android原生源码之上有一些修改,主要是添加了一些功能,但整体部分仍然是原生的架构.从零开始,逐步分析. 点开Gallery,进入该程序查看图片,在手机上看到 ...

  6. java毕业设计——基于java+TCP+UDP的局域网聊天室系统设计与实现(毕业论文+程序源码)——局域网聊天室系统

    基于java+TCP+UDP的局域网聊天室系统设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+TCP+UDP的局域网聊天室系统设计与实现,文章末尾附有本毕业设计的论文和源码下载地 ...

  7. 开源版-在线客服系统源码_网页聊天室源码_webim

    2019独角兽企业重金招聘Python工程师标准>>> WoLive是一款在线客服系统源码,支持PC Web和移动端,只需嵌入一段js代码即可快速接入.购买后可私有化部署,WoLiv ...

  8. phplivechat安卓app下载_PHP Live Chat Pro在线客服系统源码:即时聊天通迅源+手机app+教程...

    源码作者:未知 文件大小:4.53 MB 运行环境:php/mysql PHP Live Chat Pro在线客服系统源码介绍 在线客服系统为独立部署,不需要任何授权,基于PHP和MySQL ,能接入 ...

  9. Spring5源码 - 07 Spring Bean 生命周期流程 源码解读02

    文章目录 Pre 通俗流程 finishBeanFactoryInitialization Pre Spring5源码 - 06 Spring Bean 生命周期流程 概述 01 接上文 通俗流程 下 ...

  10. [并发编程] - Executor框架#ThreadPoolExecutor源码解读02

    文章目录 Pre 线程池的具体实现 线程池的创建 参数解读 corePoolSize maximumPoolSize keepAliveTime unit workQueue threadFactor ...

最新文章

  1. 基于 JWT + Refresh Token 的用户认证实践
  2. SPOJ 1811. POJ 2774 . 最大公共子串
  3. 【最详细的分析】1061 Dating (20 分)
  4. zabbix 3.0 监控windows 部署
  5. 【机器学习-西瓜书】九、K-means;聚类算法划分
  6. 几岁孩子可以学python_编程python适合多大孩子学
  7. 服务器专用影子系统,试试最牛X的影子系统!瞬间创建N个账号
  8. 什么是互联网外包公司
  9. set_drain_time和time类型数据介绍
  10. Android手机截图怎么做,手机截屏怎么弄,详细教您手机截图方法
  11. matlab fisher z变换,关于GCA统计
  12. 旋转目标检测 校准的深度特征用于目标检测SSA
  13. Win10 Chromedriver安装教程
  14. 痞子衡嵌入式:记录i.MXRT1060驱动LCD屏显示横向渐变色有亮点问题解决全过程(解答篇)...
  15. [深度学习论文笔记][Adversarial Examples] Deep Neural Networks are Easily Fooled: High Confidence Predictions
  16. 【HTMLayout学习】学习缘由、什么是HTMLayout?
  17. 中国商用显示行业“十四五”前景规划及发展方向分析报告2022-2027年版
  18. JsonMappingException Infinite recursion StackOverflowError
  19. COMSOL——相场模拟
  20. Win10技巧:Win10任务管理器知识介绍

热门文章

  1. mysql数据库名称sid_数据库sid 和数据库名
  2. php-cgi 重启,自动监测和重启 FastCGI 服务
  3. js 多维数组长度_C++申请与释放动态数组1(学习笔记:第6章 16)
  4. 用法与区别_指示代词this, that, these, those的区别和用法
  5. mysql onlibe all_MySQL Online DDL
  6. Urllib库函数、代理、爬取案例
  7. 4. 编程规范和编程安全指南--go语言
  8. YOLOv3学习笔记
  9. 一台服务器两个mysql_在一台服务器上安装两个或多个mysql的实现步骤_MySQL
  10. sift论文_卷积神经网络设计相关论文