1

I have an issue with resizing UItablecell depending on UIWebView size.

When I try to resize my Cell with heightForRowAtIndexPath method it doesn't know the size of webview because it runs before webViewDidFinishLoad.

I use webViewDidFinishLoad to resize the webView dependig the Content.

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    static NSString *CellIdentifier = @"OtherCells";

    OtherCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[OtherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    cell.WebView.delegate = self;
    NSString *ImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", [NameArray objectAtIndex:indexPath.row] ]];
    [cell.WebView loadHTMLString:[NSString stringWithFormat:@"<head><body style=”background-color:transparent”><head><body id=\"foo\">\
                                  <div><img style=\"float:left;width:100px;height:50px;margin-right:10px;margin-bottom:5px;border:1px solid gray;padding:5px;\" src=\"file://%@/\">  <p id=\"test\" style=\"color:rgb(5, 85, 173);font-size:12px;font-family:\"Trebuchet MS\"\"><b>%@</b></p>  <p id=\"test2\"  style=\"position:relative;top:-7px;font-size:11px;font-family:\"Trebuchet MS\"\">%@ <br><span id=\"time\" style=\"float:right;font-size:10px;\">%@</span></p></div></body>",ImagePath,[TitleArray objectAtIndex:indexPath.row],[SummaryArray objectAtIndex:indexPath.row],[TimeStampArray objectAtIndex:indexPath.row]] baseURL:nil];
    return cell;
}

webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    OtherCell *cell = (OtherCell *)[[webView superview] superview];
    NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"test\").offsetHeight;"];
    NSString *string2 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"test2\").offsetHeight;"];
    NSString *string3 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"time\").offsetHeight;"];
    CGFloat height = [string floatValue] + [string2 floatValue] +[string3 floatValue]+ 14;

    rowHeight=height;
    CGRect frame = [webView frame];
    frame.size.height = height;
    [webView setFrame:frame];

     NSLog(@"webViewDidFinishLoad");
     //test label after webview      
    CGRect frame1= [cell.Label frame];
    frame1.origin.y = height+2;
    [cell.Label setFrame:frame1];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cliff
  • 682
  • 8
  • 30
  • You should avoid using `UIWebView` in a `UITableViewCell`. Why do you require web view in cell? – Amar Aug 30 '13 at 07:41
  • Wrap content content in image http://s22.postimg.org/j7wbuk4bl/i_OS_Simulator_Screen_shot_Aug_30_2013_10_46_48.png – Cliff Aug 30 '13 at 07:49
  • That can be done without a web view. Check out [this question](http://stackoverflow.com/questions/5284516/how-can-i-draw-image-with-text-wrapping-on-ios) and its answer. There's also link to blog by the answerer which contains explanation. – Amar Aug 30 '13 at 07:53
  • Here's the [blog link](http://robnapier.net/blog/wrapping-text-around-shape-with-coretext-540). – Amar Aug 30 '13 at 07:56
  • i know those link are too hard for me and i am noob on xcode – Cliff Aug 30 '13 at 08:03
  • can someone translate OtherCell *cell = (OtherCell *)[[webView superview] superview]; in swift? – Yestay Muratov Jun 25 '15 at 10:14

1 Answers1

1

Calculate all the string sizes regarding their UILabel's font name, size and breaking mode.

CGSize maximumTitleLabelSize = CGSizeMake(280, 9999);
CGSize expectedFirstStringSize = [string sizeWithFont:cell.Label.font constrainedToSize:maximumTitleLabelSize lineBreakMode:cell.Label.lineBreakMode];
CGSize expectedSecondStringSize = [string2 sizeWithFont:cell.Label.font constrainedToSize:maximumTitleLabelSize lineBreakMode:cell.Label.lineBreakMode];
CGSize expectedThirdStringSize = [string3 sizeWithFont:cell.Label.font constrainedToSize:maximumTitleLabelSize lineBreakMode:cell.Label.lineBreakMode];

Then create a new frame for your cell height by the total height of your strings;

CGRect cellFrame = cell.frame;
cellFrame.size.height = expectedFirstStringSize.height + expectedSecondStringSize.height + expectedThirdStringSize.height;

And finally assign it to your cell;

cell.frame = cellFrame;

Hope this will help.

Engnyl
  • 1,380
  • 16
  • 24