1

我正在尝试根据 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;

}
4

2 回答 2

3

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

而不是 indexPathIntValue = indexPath.row;

在 webViewDidFinishLoading 方法中请使用下面的行

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

于 2012-08-16T11:24:00.157 回答
1

嗨,你为什么说:

这当然会在 insertObject 上崩溃

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

于 2012-08-16T10:16:06.600 回答