聊天界面cell的搭建

1.先创建一个CellFrame的类

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class Model;
@interface CellFrame : NSObject
// cell中内容的高
@property (nonatomic, assign, readonly) CGRect contentF;
// cell的高
@property (nonatomic, assign, readonly) CGFloat cellHeight;
@property (nonatomic, strong)Model * model;
@end

2.在这个cellFrame类中写model的set方法

- (void)setModel:(Model *)model
{
    _model = model;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
    CGSize contentSize = [model.content boundingRectWithSize:CGSizeMake(kCellWidth, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    CGFloat contentHeight = contentSize.height;
    _contentF = CGRectMake(10, 10, kCellWidth, contentHeight);
    // 当content高度小于20 cell高度都为40 大于20则在content基础上加20 增加的高度 = contentHeight + 内容距离cell上下边框的长度
    if (contentHeight < 20) {
        _cellHeight = 40;
    } else {
        _cellHeight = contentHeight + 20;
    }
}

3.创建一个自定义的cell 在.h中写一个cellFrame类的属性 以下是.m文件的代码

#import "TableViewCell.h"
#import "CellFrame.h"
#import "Model.h"

@interface TableViewCell ()
{
    UILabel  * _contentLabel;
}
@end
@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addCellSubViews];
    }
    return self;
}

- (void)addCellSubViews
{
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:15]; // 字体大小一定要和 cellFrame中的NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};一致
    [self.contentView addSubview:_contentLabel];
}

// cellFrame类的属性的set方法
- (void)setCellFrame:(CellFrame *)cellFrame
{
    _cellFrame = cellFrame;
    Model * model = cellFrame.model;
    _contentLabel.text = model.content;
    _contentLabel.frame = cellFrame.contentF;

}

4.在处理tableview要显示的数据时,要先得到model,然后给cellFrame的model属性赋值,最后把cellFrame加到数组里面

- (void)dealData
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];
    NSMutableArray * array = [NSMutableArray arrayWithContentsOfFile:plistPath];
    for (NSDictionary * dic in array) {
        CellFrame * cellFrame = [[CellFrame alloc] init];
        Model * model = [[Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        // 调用set方法
        cellFrame.model = model;
        [self.frameArray addObject:cellFrame];
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"Cell";
    TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    // 调用set方法
    cell.cellFrame = self.frameArray[indexPath.row];
    return cell;
}

5.设置row的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.frameArray[indexPath.row] cellHeight];
}

思路: 1.新建个含model属性的cellFrame类(同时含有cell中控件的CGSize和cell的高度CGFloat的属性)
2.在类中使用model属性的set方法计算出cell的高度和cell中控件的CGSize
3.在自定义cell中添加cellFrame的属性
4.在使用cellFrame属性的set方法给cell的控件frame赋值
5.处理数据把给cellFrame的model属性赋值
6.在tableView代理方法给cell的cellFrame属性赋值
7.设置row的高度

核心就是在model和cell之间多了一个frame类,需要通过两次set方法把他们连接起来。
这样写的好处是可以把对cell中控件的布局进行单独的处理,我在写聊天界面时也是通过这种方法对界面进行布局。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值