1.续前一。getter的妙用。结构清晰。
[self.view addSubview:self.searchBar];
[self.view addSubview:self.tableView];
[self.tableView addSubview:self.slimeView];
2.检测网络状态变化 ,然后是否显示网络没连接状态。
(UIView *)networkStateView//getter.
{
if (_networkStateView == nil) {
_networkStateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
_networkStateView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:199 / 255.0 blue:199 / 255.0 alpha:0.5];UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (_networkStateView.frame.size.height - 20) / 2, 20, 20)]; imageView.image = [UIImage imageNamed:@"messageSendFail"]; [_networkStateView addSubview:imageView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, 0, _networkStateView.frame.size.width - (CGRectGetMaxX(imageView.frame) + 15), _networkStateView.frame.size.height)]; label.font = [UIFont systemFontOfSize:15.0]; label.textColor = [UIColor grayColor]; label.backgroundColor = [UIColor clearColor]; label.text = NSLocalizedString(@"network.disconnection", @"Network disconnection"); [_networkStateView addSubview:label];
}
return _networkStateView;
}- (void)networkChanged:(EMConnectionState)connectionState
{
if (connectionState == eEMConnectionDisconnected) {
_tableView.tableHeaderView = _networkStateView;
}
else{
_tableView.tableHeaderView = nil;
}
}
3.CELL里自定义IMAGE和LABEL的位置。
-(void)layoutSubviews{
[super layoutSubviews];
CGRect frame = self.imageView.frame;
// [self.imageView sd_setImageWithURL:_imageURL placeholderImage:_placeholderImage];
[self.imageView imageWithUsername:_name placeholderImage:_placeholderImage];
self.imageView.frame = CGRectMake(10, 7, 45, 45);
// self.textLabel.text = _name;
[self.textLabel setTextWithUsername:_name];
self.textLabel.frame = CGRectMake(65, 7, 175, 20);
}
4.消息模型设计:文字语音,图片。发送接收者
import
define KFIRETIME 20
@interface MessageModel : NSObject
{
BOOL _isPlaying;
}
@property (nonatomic) MessageBodyType type;
@property (nonatomic, readonly) MessageDeliveryState status;
@property (nonatomic) BOOL isSender; //是否是发送者
@property (nonatomic) BOOL isRead; //是否已读
@property (nonatomic) EMMessageType messageType; // 消息类型(单聊,群里,聊天室)
@property (nonatomic, strong, readonly) NSString *messageId;
@property (nonatomic, strong) NSURL *headImageURL;
@property (nonatomic, strong) NSString *nickName;
@property (nonatomic, strong) NSString *username;
//text
@property (nonatomic, strong) NSString *content;
//image
@property (nonatomic) CGSize size;
@property (nonatomic) CGSize thumbnailSize;
@property (nonatomic, strong) NSURL *imageRemoteURL;
@property (nonatomic, strong) NSURL *thumbnailRemoteURL;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIImage *thumbnailImage;
//audio
@property (nonatomic, strong) NSString *localPath;
@property (nonatomic, strong) NSString *remotePath;
@property (nonatomic) NSInteger time;
@property (nonatomic, strong) EMChatVoice *chatVoice;
@property (nonatomic) BOOL isPlaying;
@property (nonatomic) BOOL isPlayed;
//location
@property (nonatomic, strong) NSString *address;
@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
@property (nonatomic, strong)id messageBody;
@property (nonatomic, strong)EMMessage *message;
@end
5根据是否是发送者来定头像的位置。
.-(void)layoutSubviews
{
[super layoutSubviews];
CGRect frame = _headImageView.frame;
frame.origin.x = _messageModel.isSender ? (self.bounds.size.width - _headImageView.frame.size.width - HEAD_PADDING) : HEAD_PADDING;
_headImageView.frame = frame;
[_nameLabel sizeToFit];
frame = _nameLabel.frame;
frame.origin.x = HEAD_PADDING * 2 + CGRectGetWidth(_headImageView.frame) + NAME_LABEL_PADDING;
frame.origin.y = CGRectGetMinY(_headImageView.frame);
frame.size.width = NAME_LABEL_WIDTH;
_nameLabel.frame = frame;
}