//定制单元cell
#import "TableViewCell.h"
@implementation TableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//计算大小
self.backgroundColor = [UIColor clearColor];
_imgView = [[UIImageView alloc] init ];
//添加文本信息
_txtLabel = [[UILabel alloc] init ];
_txtLabel.backgroundColor = [UIColor clearColor];
UIFont *font = [UIFont systemFontOfSize:14];
_txtLabel.font = font;
_txtLabel.numberOfLines = 0;
_txtLabel.lineBreakMode = NSLineBreakByWordWrapping;
_photo = [[UIImageView alloc]init ];
[self addSubview:_photo];
[self addSubview:_imgView];
[self addSubview:_txtLabel];
}
return self;
}
//在tableview 这样做
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"msg_cell"];
if (!cell) {
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"msg_cell"];
}
SZLMessage *msg=(SZLMessage *)_msgs[indexPath.row];
BOOL fromSelf=YES;
UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [msg.msg sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
cell.imgView.image=img;
cell.photo.image = [UIImage imageNamed:@"16.png"];
cell.txtLabel.text=msg.msg;
float width=[UIScreen mainScreen].bounds.size.width-60;
if(fromSelf){
cell.txtLabel.frame=CGRectMake(width-(cell.txtLabel.frame.size.width+20.0f), 20.0f, size.width+10, size.height+10);
cell.imgView.frame = CGRectMake(width-20-(cell.txtLabel.frame.size.width+20.0f), 14.0f, cell.txtLabel.frame.size.width+30.0f, cell.txtLabel.frame.size.height+20.0f);
cell.photo.frame= CGRectMake(width, 10, 50, 50);
}else{
cell.txtLabel.frame=CGRectMake(85, 20.0f, size.width+10, size.height+10);
cell.imgView.frame = CGRectMake(65, 14.0f, cell.txtLabel.frame.size.width+30.0f, cell.txtLabel.frame.size.height+20.0f);
cell.photo.frame= CGRectMake(10, 10, 50, 50);
}
return cell;
}
高仿微信和qq气泡聊天(整合精简版)
最新推荐文章于 2017-05-23 10:41:59 发布
本文介绍如何在iOS应用中自定义消息单元格(cell),包括设置背景图片、文本标签及图片视图,并根据不同消息来源调整布局。
828

被折叠的 条评论
为什么被折叠?



