sakkaras / SKSTableView

An expandable table view.
MIT License
446 stars 115 forks source link

Auto calculate height for rows and sub-rows #33

Open brbgyn opened 9 years ago

brbgyn commented 9 years ago

Hi, i implemented the auto-heigh for rows and sub-rows.

Hope to see it on project and help other developers.

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
    cell.textLabel.numberOfLines = 0;
...
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
...
    cell.textLabel.numberOfLines = 0;
...
}

- (CGFloat)tableView:(SKSTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *item = self.contents[indexPath.section][indexPath.row][0];

    CGFloat textHeight = [self getLabelHeightForText:item andWidth:tableView.bounds.size.width];
    return textHeight + 20;
}

-(CGFloat)tableView:(SKSTableView *)tableView heightForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *item = self.contents[indexPath.section][indexPath.row][indexPath.subRow];

    CGFloat textHeight = [self getLabelHeightForText:item andWidth:tableView.bounds.size.width - 20];
    return textHeight + 20;
}

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
    CGSize maximumSize = CGSizeMake(labelWidth, 10000);
    CGSize labelHeighSize = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation];
    return labelHeighSize.height;
}
mohreda commented 8 years ago

I tried to use the previous code; its only working with three lines not more; moreover when first selection its only one line; please help

brbgyn commented 8 years ago

You can see it in action in my app: https://itunes.apple.com/br/app/tesouro-direto-br-renda-fixa/id859955280?mt=8

From the main menu, select "O Tesouro Direto" or "Perguntas Frequentes"

mohreda commented 8 years ago

ahhh; thanks its my mistake; it's working now with me Perfect;

brbgyn commented 8 years ago

Great !

mohreda commented 8 years ago

sorry; but i faced new problem; when i select first time auto calculation for height for sub row not working; but with second selection or when close the row and open again its working perfect; can you help

brbgyn commented 8 years ago

I don't have the code here with me, but maybe if you try to force recalculation on view did appear, or to programmatically touch on the first row to force the row height recalculation, i don't know.. Maybe one of this could work

mohreda commented 8 years ago

ok; i\ll try thx for your reply