关于UITablewViewCell 中cell高度可变,重用时数据发生错误问题

        在我们使用UITablewViewCell时,在很多情况下,我们不仅需要固定高度的cell来进行数据和界面的布局,有时候根据数据的不同,我们还需要有高度不同的Cell来进行数据的存放和界面的布局。 



比如这样一个由换行Label来决定cell高度的UITablewViewCell, 如果出现在滑动过程中出现数据错乱的现象,我的解决办法是,在UITablewViewCell调用它的代理方法之前,这时候我们已经拿到了cell上面所需要存放的数据,这时候我们就可以根据这些数据来计算每一条数据所对应的cell的高度。这时候我们就可以提前控制cell的高度,这时候无论UITablewViewCell调用代理方法的顺序是怎么样的,我们都能够保证每一个cell所对应的数据是正确的。这时候我们需要创建一个为cell存放数据源的Model.

具体实现代码如下:

在所创建的Model中要存在一个高度的属性:

@property (nonatomic, strong) NSString *testStr;


@property (nonatomic, assign) float height;


- (instancetype) initWithString:(NSString *) text;

- (instancetype) initWithString:(NSString *) text

{

    if (self = [super init]) {

        self.testStr = text;

        //获取字符串进行换行之后的高度

        CGSize titleSize = [CommonClass stringSizeWithStr:text maxSize:CGSizeMake(SCREENWIDTH - 30, MAXFLOAT) strFont:[UIFont systemFontOfSize:14.0] lineSpace:0];

        self.height = titleSize.height + 30;

    }

    return self;

}


在控制器中先对model进行传值,然后正常调用相应的代理方法:

 TestModel * model0 = [[TestModel alloc] initWithString:@"苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model1 = [[TestModel alloc] initWithString:@"苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model2 = [[TestModel alloc] initWithString:@"苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model3 = [[TestModel alloc] initWithString:@"苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model4 = [[TestModel alloc] initWithString:@"苹果周五提交给的文件显示"];

    TestModel * model5 = [[TestModel alloc] initWithString:@"苹果周五提交给监管苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model6 = [[TestModel alloc] initWithString:@"苹果周五提交给监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model7 = [[TestModel alloc] initWithString:@"苹果周五提交姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    TestModel * model8 = [[TestModel alloc] initWithString:@"苹果周五提交给"];

    TestModel * model9 = [[TestModel alloc] initWithString:@"苹果周五提监管部门的文件显示,苹果CEO蒂姆·库克本周出售了价值3600万美元的苹果股份。苹果周五提交给监管部门的文件显示苹果周五提交给监管部门的文件显示"];

    

    _dataSourceArr = @[model0, model1,model2, model3,model4, model5,model6, model7,model8, model9,model0,model1,model2,model4];


#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    TestModel *model = [_dataSourceArr objectAtIndex:indexPath.row];

    static NSString *cellString = @"inder";

    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];

    if(!cell)

    {

        cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString];

    }

    [cell setCellData:model];

    return cell;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataSourceArr.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    TestModel *model = [_dataSourceArr objectAtIndex:indexPath.row];

    return model.height;

}


在cell中同样是用model中所获取的高度进行设置:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])

    {

        _testLabel = [[UILabel alloc] init];

        _testLabel.backgroundColor = [UIColor orangeColor];

        _testLabel.font = [UIFont systemFontOfSize:14.0];

        _testLabel.textAlignment = NSTextAlignmentLeft;

        _testLabel.numberOfLines = 0;

        [self.contentView addSubview:_testLabel];

    }

    return self;

}


- (void)setCellData:(TestModel *)model;

{

    _testLabel.text = model.testStr;

    [_testLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(15);

        make.right.equalTo(-15);

        make.top.equalTo(0);

        make.height.equalTo(model.height - 30);

    }];

}


这只是一种最近刚摸索出来的一种感觉比较合乎逻辑的方法,如果有错误的地方希望大家见谅并能提出意见进行修改!总之一句话在获取数据的同时就计算好cell所需要的高度。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值