2013-06-26 49 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

回答

相关问题