UILabel SizeToFit的UITableView在滚动时会变得混乱 [英] UITableView with UILabel SizeToFit get mess when Scrolling

查看:231
本文介绍了UILabel SizeToFit的UITableView在滚动时会变得混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我的 tableview 有问题,我用 uilabel 制作单元格 SizeToFit 然后计算 UILabel 高度设置单元格高度一切正常,除非我滚动 tableView 文本变得奇怪,如每行一个字符:

Hi everyone I have problem with my tableview, i make Cell with uilabel with SizeToFit and then calculate the UILabel Height for set the cell Height everything work well except when i scrolling my tableView the text get weird like one char per line:

我的 TableViewVell 方法是:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [someArray objectAtIndex:indexPath.row];
// Set the UILabel to fit my text;
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
messageLabel.numberOfLines = 0;
[messageLabel sizeToFit];


return cell;   
}

单元格高度保持正确大小只有<$ c的问题$ c> UILabel ...
这是加载视图时的结果:

The Cell Height stay in the correct Size the problem only with the UILabel ... this the result when i load the view:

第一张截图

这是在我开始滚动TableView之后:

And this is after i start scroll the TableView:

第二个截图

任何帮助???

推荐答案

你应该继承你的单元格并清理UILabel

you should probably subclass your cell and clean the UILabel on

 override func prepareForReuse() {
    super.prepareForReuse()
    self.label.text = nil
}

这篇关于UILabel SizeToFit的UITableView在滚动时会变得混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆