SKSTableView icon indicating copy to clipboard operation
SKSTableView copied to clipboard

Auto calculate height for rows and sub-rows

Open brbgyn opened this issue 9 years ago • 7 comments

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;
}

brbgyn avatar Apr 06 '15 21:04 brbgyn

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

mohreda avatar Mar 07 '16 00:03 mohreda

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"

brbgyn avatar Mar 07 '16 02:03 brbgyn

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

mohreda avatar Mar 07 '16 02:03 mohreda

Great !

brbgyn avatar Mar 07 '16 02:03 brbgyn

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

mohreda avatar Mar 14 '16 23:03 mohreda

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

brbgyn avatar Mar 14 '16 23:03 brbgyn

ok; i\ll try thx for your reply

mohreda avatar Mar 15 '16 00:03 mohreda