简体   繁体   中英

Dynamic cell height based on content ios

I working on a app which loads the posts of user on facebook. All this info is put into a tableview. The cells are expendable when you tap on it. For now there is only text in it. I calculate the height of the text so I know how high the cell needs to be when expanded.

Problem which I having is that there needs to be more than only text in it. If the user posts a video on facebook the video will also be embedded in the cell. But since the height is calculated based on the text it leaves no room for the embedded video. I can add a margin in the cell but this margin will be applied to al the cell then.

This is the code which I am using right now:

//Function executes everything within when a certain row in tapped/selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    [self.tableView reloadData];
    [self stopLoading];
    if (selectedIndexPath == indexPath) 
    {         
        selectedIndexPath = nil;
    }
    else 
    {        
        selectedIndexPath = indexPath;
    }          
    [self.tableView deselectRowAtIndexPath : indexPath animated : YES];
    [tableView beginUpdates];
    [tableView endUpdates];
}

//Change heigth of the selected row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)) 
    {
        labelSize = [[result2 objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13.0] 
                                                                  constrainedToSize:CGSizeMake(220.0f, MAXFLOAT) 
                                                                lineBreakMode:UILineBreakModeWordWrap];
        return labelSize.height + 136;

    }
    return 68.0;
}  

So what I need to know. How can I calculate to height of the cell based on al the content? and not only the text.

Would be great if somebody can help with this!

Thnx in advance

Actually.. it was pretty simple. Just had to add the following in heightForRowAtIndexPath

    NSString *video = [fbSource objectAtIndex:indexPath.row] ; 
    if (video != @"empty") { return labelSize.height + 136; } 
    return labelSize.height + 36;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM