2012-08-16 74 views
1

我試圖根據webview的高度有一個單元格的自定義高度。根據webviews內容調整UITableViewCell與WebViews

現在我嘗試將webview的frame.size.height添加到一個NSMutableArray,當然它在insertObject上崩潰(我在這裏試過的解決方案很sl))。

我該如何做到這一點?

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; 

} 

回答

3

請設置cell.feedWebView.tag = indexPath.row;

而不是indexPathIntValue = indexPath.row;

而在webViewDidFinishLoading方法請使用下面線

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

1

嗨,爲什麼你說:

其上insertObject

當然崩潰的其實應該不會崩潰。確保sizeOfWebViewCellNSMutableArray,使用方法addObject並檢查您的實例是否已正確分配和初始化。