开发者

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:(UITableV开发者_运维知识库iew *)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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜