UITableCell中的WebView具有调整大小单元格问题

时间:2013-08-30 07:32:44

标签: ios uitableview uiwebview webview

我在调整UItablecell大小时遇到​​问题,具体取决于UIWebView大小。

当我尝试使用Cell方法调整heightForRowAtIndexPath的大小时,它不知道webview的大小,因为它在webViewDidFinishLoad之前运行。

我使用webViewDidFinishLoad来调整webView的大小,取决于内容。

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

1 个答案:

答案 0 :(得分:1)

计算有关其UILabel字体名称,大小和中断模式的所有字符串大小。

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

然后根据字符串的总高度为单元格高度创建一个新框架;

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

最后将它分配给你的手机;

cell.frame = cellFrame;

希望这会有所帮助。