根据webviews内容使用WebViews调整UITableViewCell的大小

时间:2012-08-16 10:04:36

标签: iphone objective-c ios uitableview

我正在尝试根据webview的高度在单元格上设置自定义高度。

现在我尝试将webview的frame.size.height添加到NSMutableArray当然会在insertObject上崩溃(我在这里尝试的解决方案非常草率)。

我如何做到这一点?

int indexPathIntValue;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifer = @"PictureCell";

    InspirationFeedCell *cell;
    cell = (InspirationFeedCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifer];
    NSBundle *mainBundle = [NSBundle mainBundle];
    if (cell == nil) {
        [mainBundle loadNibNamed:@"PictureCell" owner:self options:nil];
        cell = self.feedCell;
        self.feedCell = nil;
    }

    indexPathIntValue = indexPath.row;

    //Here I deleted some code that fetches descriptionWebString

    [cell.feedWebView setDelegate:self];
    [cell.feedWebView loadHTMLString:descriptionWebString baseURL:nil];

    return cell;

}

-(void) webViewDidFinishLoad:(UIWebView *)webView {

    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;

    int heightOfWebViewInt = frame.size.height;
    [sizeOfWebViewCell insertObject:[NSNumber numberWithInt:heightOfWebViewInt] atIndex:indexPathIntTest];
    DLog(@"numberSizeInValue: %d", heightOfWebViewInt);

    if (!alreadyUpdated) {
        alreadyUpdated = YES;
        [inspirationFeedTable reloadData];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < [sizeOfWebViewCell count] && [sizeOfWebViewCell count] != 0) {

        NSNumber *numberValueOfHeight = [sizeOfWebViewCell objectAtIndex:indexPath.row];


        int rowAtHeight = numberValueOfHeight.integerValue;

        return rowAtHeight;

    }
    return 360;

}

2 个答案:

答案 0 :(得分:3)

请设置cell.feedWebView.tag = indexPath.row;

而不是indexPathIntValue = indexPath.row;

在webViewDidFinishLoading方法中请使用以下行

[sizeOfWebViewCell insertObject:[NSNumber numberWithInt:heightOfWebViewInt] atIndex:webView.tag];

答案 1 :(得分:1)

你好,你为什么这么说:

  

当然在insertObject

上崩溃了

实际上它不应该崩溃。确保sizeOfWebViewCellNSMutableArray,使用方法addObject并检查您的实例是否已正确分配和初始化。