2013-06-26 52 views
-1

如何將UITableView單元格的大小更改爲NSString的大小?我已經嘗試了以下內容,並且單元格的大小略有變化,但不是文本的整個高度。ios:將單元格高度更改爲文本大小

我:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *str = [_recipe.ingredients objectAtIndex:indexPath.row]; 

    CGRect screenBound = [[UIScreen mainScreen] bounds]; 
    CGSize screenSize = screenBound.size; 
    CGFloat screenWidth = screenSize.width; 

    UIFont *cellFont = [UIFont systemFontOfSize:14]; 
    CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT); 
    CGSize labelSize = [str sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height +35; 
} 

和:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 



    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    if(indexPath.section == 0){ 
     cell.textLabel.text = [_recipe.ingredients objectAtIndex:indexPath.row]; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15]; 
     cell.textLabel.numberOfLines=1; 
     cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation; 

    } 

    //cell.textLabel.text = [NSString stringWithFormat:@"Cell Row #%d", [indexPath row]]; 

    return cell; 

} 

我得到兩次警告UILineBreakModeTailTruncated折舊。

如何獲取要換行的文字。

+3

是的,實現你的評論線。 – Wain

+0

當「官方」技術由於某種原因無法正常工作時,我已經採取了將字符串寫入虛擬標籤並採用該標籤的大小。 –

+0

我想通了,只是將折舊值更改爲NSLineBreakByWordWrapping – wwjdm

回答

相關問題