2013-11-07 3 views
1

У меня есть UILabel в ячейке, и мне нужно динамически менять высоту UILabel. Таким образом, ячейка представляет собой xib-файл с высотой 44 и меткой 240x44 и маркировку автосогласования по горизонтали и по вертикали. Размер шрифта - 15.0f. Количество строк = 0, обертывание по слову.Динамическая высота ячейки неправильная иногда

Так что у меня метод

+ (CGFloat) cellHeightForString: (NSString *) string 
{ 
    CGFloat cellHeight = [string sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(240.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height; 
    return cellHeight < 44.0f ? 44.0f : cellHeight; 
} 

но высота ячейки иногда меньше, так что не все строки текста отображаются в ячейке. Не могу понять, что я делаю неправильно. Любая помощь?

+0

проверить размер шрифта струны, которая была показана в виде таблицы. Я думаю, что это больше 15! –

+0

Система 15.0 в xib и [UIFont systemFontOfSize: 15.0f] в размереWithFontMethod – ShurupuS

ответ

0

Попробуйте это,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
// add your code snippet here 
// dynamically change the label height, 
     CGSize textSize = { 
     200.0, // limit width 
     20000.0 // and height of text area 
    }; 

    CGSize contentSize = [yourText sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping]; 
    CGFloat contentHeight = contentSize.height < 36? 36: contentSize.height; // lower bound for height 

    CGRect labelFrame = [yourLabel frame]; 
    yourLabel.size.height = contentHeight; 
    [yourLabel setFrame: labelFrame]; 
    [yourLabel setText:yourText]; 
    return cell; 
} 

Динамически изменить высоту ячейки,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    YourCell *currentCell = (YourCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath]; 

// change hard coded value based on your cell alignment 

    int cellLength=[currentCell.yourLabel.text length]/42; 
    cellLength = cellLength*22 > 200 ? cellLength*22 : 200; 
    CGSize constraint = CGSizeMake((200 - 10), cellLength); 
    CGSize size1 = [cellText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; 
    return 220+size1.height; 
} 
Смежные вопросы