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

ответ

0

Вы хотите использовать [str sizeWithFont:]. Существуют и другие варианты этого метода, которые учитывают режим ограниченной ширины и разрыва строки. Посмотрите здесь, чтобы найти что-то, что будет работать: http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html