源码-0205-02--聊天布局
生活随笔
收集整理的這篇文章主要介紹了
源码-0205-02--聊天布局
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
還真是失敗,搞了兩天遇到了布局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--聊天布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cf776G.Sherlock and
- 下一篇: Ubuntu中基于QT的系统网线连接状态