** 准备工作: **
- 新建DLmessage模型类文件.
- 新建DLMessageCell类文件,将storyboard中cell的class改为DLMessageCell.设置上面cell的重用标识为messageMe,下面cell的重用标识为messageOther.
- 新建DLMessageViewController类文件,将storyboard中的控制器class改为DLMessageViewControlle.
1.将所需控件布局到storyboard,设置控件的图片等属性,并设置控制器为tableView的数据源和代理.
如下图:
2.通过autoLayout设置每个控件的布局,其中button的宽度属性大于等于 60 小于等于 240 ,这样button里面的titleLabel属性就能跟随内容长度变化,从而也就决定了button的宽度长短.在自定义cell类的awakeFromNib方法中设置button中titleLabel的numberOfLines为零,这样就实现了Button内容的自动换行功能.
- (void)awakeFromNib{
//设置titleLabel达到button最大宽度时自动换行
self.messageButton.titleLabel.numberOfLines = 0;
}
3.将控件连线DLMessageCell类扩展中.不同cell可以将相同的属性共用.
4.创建DLmessage模型中的成员属性,使其能够加载并记录plist文件数据.
DLmessage.h文件
#import <UIKit/UIKit.h>
typedef enum {
DLMessageTypeMe = 0,
DLMessageTypeOther = 1
} DLMessageType;
@interface DLMessage : NSObject
@property(nonatomic,strong) NSString *text;
@property(nonatomic,strong) NSString *time;
@property(nonatomic,assign) DLMessageType type;
//记录行高,取出cell中高度的最大值来赋值
@property(nonatomic,assign) CGFloat cellHeight;
//记录发送时间是否显示
@property(nonatomic,assign,getter=isHideTime) BOOL hideTime;
//构造方法,方便外界传入plist文件
+ (instancetype)messageWithDict:(NSDictionary *)dict;
@end
DLmessage.m文件
#import "DLMessage.h"
@implementation DLMessage
+ (instancetype)messageWithDict:(NSDictionary *)dict{
DLMessage *message = [[self alloc] init];
//KVC
[message setValuesForKeysWithDictionary:dict];
return message;
}
@end
5.在DLMessageCell.h文件中新建模型类成员属性,在.m中重写set方法,将数据传给子控件
DLMessageCell.h文件
#import <UIKit/UIKit.h>
@class DLMessage;
@interface DLMessageCell : UITableViewCell
@property(nonatomic,strong) DLMessage *message;
@end
DLMessageCell.m文件
#import "DLMessageViewCell.h"
#import "DLMessage.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 DLMessageCell ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *messageButton;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation DLMessageCell
- (void)awakeFromNib{
//设置换行
self.messageButton.titleLabel.numberOfLines = 0;
}
- (void)setMessage:(DLMessage *)message{
_message = message;
//隐藏时间
if (message.hideTime) {
self.timeLabel.hidden = YES;
[self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(0);
}];
}else{//显示时间
self.timeLabel.hidden = NO;
self.timeLabel.text = message.time;
[self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(21);
}];
}
//设置控件内容
[self.messageButton setTitle:self.message.text forState:UIControlStateNormal];
//强制更新
[self layoutIfNeeded];
//修改button的高度
[self.messageButton updateConstraints:^(MASConstraintMaker *make) {
CGFloat buttonH = self.messageButton.titleLabel.frame.size.height + 30;
make.height.equalTo(buttonH);
}];
[self layoutIfNeeded];
//获取button与nameView的最大值,设置最大值为cell的高度
CGFloat buttonY = CGRectGetMaxY(self.messageButton.frame);
CGFloat iconViewY = CGRectGetMaxY(self.iconView.frame);
self.message.cellHeight = MAX(buttonY, iconViewY) + 5;
}
@end
6.在DLMessageViewController中新建数组,加载模型数据,同时实现tableView的数据源方法,需遵守UITableViewDataSource,UITableViewDelegate协议.
#import "DLMessageViewController.h"
#import "DLMessageCell.h"
#import "DLMessage.h"
@interface DLMessageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) NSArray *messages;
@property (weak, nonatomic) IBOutlet UITextField *messageFeild;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttomSpacing;
@end
@implementation DLMessageViewController
- (NSArray *)messages{
if (_messages == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *messageArrM = [NSMutableArray array];
DLMessage *lastMessage = nil;
for (NSDictionary *dict in dictArr) {
DLMessage *message = [DLMessage messageWithDict:dict];
message.hideTime = [lastMessage.time isEqualToString:message.time];
[messageArrM addObject:message];
lastMessage = message;
}
_messages = messageArrM;
}
return _messages;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
//返回cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.messages.count;
}
//设置cell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//取出模型的数据
DLMessage *message = self.messages[indexPath.row];
/*根据模型中type的值来设置数据(方法1)
DLMessageCell *cell = [[DLMessageCell alloc] init];
if (message.type == DLMessageTypeMe) {
cell = [tableView dequeueReusableCellWithIdentifier:@"messageMe"];
}else if(message.type == DLMessageTypeOther){
cell = [tableView dequeueReusableCellWithIdentifier:@"messageOther"];
}
cell.message = self.messages[indexPath.row];
return cell;
*/
// 根据模型中type的值来设置数据(方法2)
NSString *ID = (message.type == DLMessageTypeMe)? @"messageMe" : @"messageOther";
DLMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell.message = message;
return cell;
}
//返回cell的估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
DLMessage *message = self.messages[indexPath.row];
return message.cellHeight;
}