2015-09-05 39 views
1

嗨我在我的UILabel字段上使用sizetofit,它工作正常,当我加载tableview控制器,但然后开始切割的字母和包装到另一行时,当我上下滚动。下面是我的cellForRowAtIndexPath使用代码:UItableviewcontroller SizeToFit不工作在UILabel字段

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

    // Configure the cell... 

    // Load and display subject photo image 
    UIImageView *subjectImageView = (UIImageView *)[cell viewWithTag:200]; 
    subjectImageView.image = [UIImage imageNamed:subjectImages[indexPath.row]]; 


    // Load and display subject label 
    UILabel *subjectLabel = (UILabel *)[cell viewWithTag:201]; 
    subjectLabel.text = [subjectItems objectAtIndex:indexPath.row]; 
    [subjectLabel sizeToFit];// call this to fit size of the label according to text 



    return cell; 
} 

,这里是我的tableviewcontroller的屏幕截图的渊源加载:

enter image description here

这里滚动起来后,下您可以看到的描述有被切断

enter image description here

感谢您的帮助,您可以摹以解决这个问题。

干杯

卡森

+0

显示'cellForRowAtIndexPath:'方法。 – rmaddy

+0

@rmaddy见上面我现在正在显示完整的cellForRowAtIndexPath – Kitcc

+0

在'sizeToFit'后面试试'setNeedDisplay' – anhtu

回答

1

使用sizeToFitUILabel可引起这样一团糟在UITableView与再利用细胞。我能想到的一个解决方案是在您拨打sizeToFit之前为您的标签宽度设置框架的宽度,并且每次将文本分配给标签时都必须执行此操作。它应该看起来像这样:

CGRect frame = subjectLabel.frame; 
frame.size.width = 100.0; //try to adjust this value 
subjectLabel.frame = frame; 
[subjectLabel sizeToFit]; 
+1

非常感谢,这就解决了它:-) – Kitcc