首页 > 源码-0205-02--聊天布局

源码-0205-02--聊天布局

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

聊天布局

//
//  XMGChatingViewController.m
//  07-聊天布局
#import "XMGChatingViewController.h"
#import "XMGMessage.h"
#import "XMGMessageCell.h"@interface XMGChatingViewController () 
@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 - 
- (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 - 
- (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 
@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 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

更多相关:

  • 1.CoreLocation框架使用前提: #import CoreLocation框架中所有数据类型的前缀都是CL ,CoreLocation中使用CLLocationManager对象来做用户定位 2.CLLocationManager的常用操作: 开始用户定位 - (v...

  • protobuf的数据类型,有最简单的那种数据类型,就是一个文件中,定义了一个message 可以在一个文件中定义两个message,两个message之间是没有关联的可以在一个文件中,定义两个message,其中一个是简单的,他作为了另个一的字段 message SearchResponse {repeated Result re...

  • @Ignore 用法很简单, 如果你的测试用例还没有准备好而不想被执行, 又不想删掉或注释掉, 可以使用 @Ignore 标注来忽略测试。 方法一旦用 @Ignore 注解了将不会被执行. 如果一个类用 @Ignore 注解了 他下面的所有测试方法将不会被执行. 看个应用 Create a Class Create a java...