2013-04-03 116 views
4

我正在使用此代碼來調整維護單元格標題的標籤大小。問題是單元格標籤不會調整大小,直到我滾動表格。細胞需要消失才能正確調整其標籤大小。我能做些什麼來讓所有的單元格標籤都能很好地調整大小,而不必事先滾動?單元格標籤不調整大小直到表滾動

感謝您的閱讀。

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

    ExampleTableViewCell *cell = (ExampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExampleTableViewCell"]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.title.text = [self.test objectAtIndex:[indexPath row]]; 

    //Calculates the expected size based on the font and linebreak mode of the label. 
    CGSize maximumLabelSize = CGSizeMake(200, [UIScreen mainScreen].bounds.size.width); 

    CGSize expectedLabelSize = [cell.title.text sizeWithFont:cell.title.font constrainedToSize:maximumLabelSize lineBreakMode:cell.title.lineBreakMode]; 

    // Adjusts the label the the new height. 
    CGRect newFrame = cell.title.frame; 
    newFrame.size.height = expectedLabelSize.height; 
    cell.title.frame = newFrame; 

    return cell; 
} 
+0

郵報委託方法heightForRowAtIndexPath代碼.. – Apurv

+0

有我只是在 –

回答

0

嘗試寫同樣的代碼在

willDisplayCell的UITableViewController

的方法,另一種方法是在....你ExampleTableViewCell類實現

- (void)layoutSubviews{ 
} 

設置你的標題的框架以上功能。它應該工作

+0

謝謝細胞的最大尺寸,但仍然沒有工作 –

+0

我已經編輯我的答案用另一種方法... –

+0

它的工作原理,但只有第一次。當我以前訪問過這個視圖時,我仍然遇到同樣的問題 –

0

您必須實現的方法heightForRowAtIndexPath和返回單元格的高度,然後你的問題解決了:-)

相關問題