2015-09-05 66 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