2014-08-27 27 views
0

我正在故事板中工作。我有從tableViewVC繼承的視圖控制器。在那個vc我有一個或兩行文本的單元格。如果我有一行,我只是將文本分配給自定義單元格中的自定義textLabel。如果我有兩條線,則調整標籤框的大小。但是在加載表時它不起作用。我只能在向上/向下滾動tableView後才能在單元格中看到我調整大小的標籤。我該如何解決它?標籤的框架大小不會在tableView單元格中更改

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
OptionItem *item = _currentOptionsList[indexPath.row]; 

static NSString *CellIdentifier = @"OptionSimpleCellIdentifier"; 
OptionSimpleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

cell.titleLabel.text = item.title; 
cell.haveDetail = item.haveDetailItems; 
cell.selectedSimpleOption = item.simpleSelected; 
if (item.haveSelectedDetailItem) 
{ 
    cell.detailLabel.text = [item.detailItems[item.indexOfSelecteDetaildItem] title]; 

} else { 
    cell.detailLabel.text = nil; 
    cell.detailImageView.image = nil; 
} 


CGFloat textHeight = [Calculatons getLabelHeightForText:item.title andWidth:225 withFont:kFontListCell withMaxHeight:0];//19 - 39 
CGRect rect = cell.titleLabel.frame; 

NSLog(@"textHeight = %f cell.height = %f titleLabel.height %f \n",textHeight, cell.frame.size.height, cell.titleLabel.frame.size.height); 

if (textHeight > 21) 
{ 
    rect.size.height = 41; 
    cell.titleLabel.frame = rect; 
} 


    //nothing of this not works 
    [cell.titleLabel layoutIfNeeded]; 
    [cell.titleLabel setNeedsLayout]; 
[cell.titleLabel setNeedsUpdateConstraints]; 
    [cell.titleLabel setNumberOfLines:0]; 

return cell; 
} 

http://s23.postimg.org/8tszefzbf/Screen_Shot_2014_08_27_at_10_01_10_PM.png

第一屏幕後,我推VS.第二個屏幕是我上下滾動表格後顯示的內容。

我需要第二個屏幕後,我推VC。

回答

1

嘗試在代碼中使用此項,緊接在單元格初始化之後,而不是在將值設置爲標籤之後。

CGFloat textHeight = [Calculatons getLabelHeightForText:item.title andWidth:225 withFont:kFontListCell withMaxHeight:0];//19 - 39 
CGRect rect = cell.titleLabel.frame; 

NSLog(@"textHeight = %f cell.height = %f titleLabel.height %f \n",textHeight, cell.frame.size.height, cell.titleLabel.frame.size.height); 

if (textHeight > 21) 
{ 
    rect.size.height = 41; 
    cell.titleLabel.frame = rect; 
} 
相關問題