---------- CZMessageCell.h ----------
#import <UIKit/UIKit.h>
@class CZMessageFrame;
@interface CZMessageCell : UITableViewCell
@property (strong, nonatomic) CZMessageFrame *messageFrame;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
---------- CZMessageCell.m ----------
#import "CZMessageCell.h"
#import "CZMessage.h"
#import "CZMessageFrame.h"
#import "UIImage+CZHelp.h"
@interface CZMessageCell ()
@property (weak, nonatomic) UILabel *timeLabel;
@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UIButton *textBtn;
@end
@implementation CZMessageCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"cell";
CZMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil)
{
cell = [[CZMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
UILabel *timeLabel = [[UILabel alloc] init];
timeLabel.font = CZTimeFont;
timeLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:timeLabel];
self.timeLabel = timeLabel;
UIImageView *iconView = [[UIImageView alloc] init];
iconView.layer.cornerRadius = 8;
iconView.clipsToBounds = YES;
[self.contentView addSubview:iconView];
self.iconView = iconView;
UIButton *textBtn = [[UIButton alloc] init];
textBtn.titleLabel.font = CZTextFont;
textBtn.titleLabel.numberOfLines = 0;
[self.contentView addSubview:textBtn];
self.textBtn = textBtn;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setMessageFrame:(CZMessageFrame *)messageFrame
{
_messageFrame = messageFrame;
CZMessage *msg = messageFrame.message;
if (msg.hiddenTime) {
self.timeLabel.hidden = YES;
} else {
self.timeLabel.hidden = NO;
self.timeLabel.text = msg.time;
self.timeLabel.frame = messageFrame.timeF;
}
self.iconView.image = [UIImage imageNamed:(msg.type == CZMessageTypeMe) ? @"me" : @"other"];
self.iconView.frame = messageFrame.iconF;
[self.textBtn setTitle:msg.text forState:UIControlStateNormal];
self.textBtn.frame = messageFrame.textF;
NSString *normal, *high;
if (msg.type == CZMessageTypeOther)
{
normal = @"chat_recive_nor";
high = @"chat_recive_press_pic";
[self.textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
} else {
normal = @"chat_send_nor";
high = @"chat_send_press_pic";
[self.textBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
self.textBtn.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
[self.textBtn setBackgroundImage:[UIImage resizedImageNamed:normal] forState:UIControlStateNormal];
[self.textBtn setBackgroundImage:[UIImage resizedImageNamed:high] forState:UIControlStateHighlighted];
}
@end
原文:http://www.cnblogs.com/lixiang2015/p/4719811.html