2015-02-05 6 views
1

Динамическое увеличение динамической высоты UITableViewCell в UITableViewController с помощью UILabel.Динамическая высота UITableViewCell

+1

Для получения высоты ярлыка необходимо использовать NSAttributedString. – kb920

+1

http://stackoverflow.com/questions/15609720/dynamically-change-height-of-cells-in-objective-c – kb920

+0

Можете учесть, почему вы хотите использовать метку для высоты @madan gupta –

ответ

3

Для использования динамической tableViewCell высоты этого.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGSize labelHeight = [self heigtForCellwithString:yourLabel.text withFont:yourLabel.font]; 
    return labelHeight.height; // the return height + your other view height 
} 

-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{ 
CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width 
    NSDictionary *attributes = @{NSFontAttributeName: font}; 
    CGRect rect = [stringValue boundingRectWithSize:constraint 
             options:   (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) 
            attributes:attributes 
             context:nil]; 
    return rect.size; 

} 
+0

Да, с небольшими изменениями ... его работа ... чем –

+0

@ Мадангупта: если все в порядке, примите ответ. –

+0

Это не работает для меня. initWithObjects: forKeys: count:]: попытка вставить нулевой объект из объектов [0] '- возможно, ничего не добавляет к атрибутам NSDictionary *. Кто-нибудь еще думает, что это работает? – durazno

2

Вы можете использовать метод делегата

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 44.0f; 
} 

в вышеуказанном способе вы можете вернуть одну переменную CGFloat, которые имеют динамическую высоту ячейки для конкретного индекса UITableView

2
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UIFont *font = [UIFont systemFontOfSize:20]; 
NSDictionary *userAttributes = @{NSFontAttributeName: font, 
           NSForegroundColorAttributeName:  [UIColor blackColor]}; 
NSString *text = @"Your label text"; 

CGSize textSize = [text sizeWithAttributes: userAttributes]; 

ht = 20; 

if (textSize.width > [FunctionUtils getViewWidth]) { 

    ht = (textSize.width/([FunctionUtils getViewWidth])); 
    ht = (ceil(ht))*20+35; 
//20 font size 
} 

return ht; 
}