在我们使用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);
}];
}