2012-11-11 43 views
3

我知道這已被要求死亡,我向你保證我已經通讀了關於此主題的每個線索。到目前爲止,我沒有任何工作。這是我迄今爲止......我瞭解2000年的高度有點荒謬,但我試圖在webView中看到整篇文章。根據內容調整UITableViewCell內部的UIWebView的大小

- (void)viewDidLoad { 

// Super 
[super viewDidLoad]; 

_date = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                 dateStyle:NSDateFormatterShortStyle 
                 timeStyle:NSDateFormatterShortStyle]; 

CGRect frame = _descriptionWebView.frame; 
frame.size.width = 320; 
frame.size.height = 2000; 
_descriptionWebView.frame = frame; 
_descriptionWebView = [[UIWebView alloc] initWithFrame:frame]; 
_descriptionWebView.delegate = self; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
// Return the number of sections. 
return 2; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
switch (section) { 
    case 0: return 3; 
    default: return 1; 
} 

} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// Get cell 
static NSString *CellIdentifier = @"CellA"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 
cell.textLabel.textColor = [UIColor blackColor]; 
cell.textLabel.font = [UIFont systemFontOfSize:15]; 

    // Display 
    switch (indexPath.section) { 
     case SectionHeader: { 

      // Header 
      switch (indexPath.row) { 
       case SectionHeaderTitle: 
        cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
        cell.textLabel.text = _blogTitle; 
        cell.textLabel.numberOfLines = 2; // Multiline 
        break; 
       case SectionHeaderDate: 
        cell.textLabel.text = _date; 
        break; 
       case SectionHeaderURL: 
        cell.textLabel.text = [NSString stringWithFormat:@"By %@",_author]; 
        cell.textLabel.textColor = [UIColor grayColor]; 
        break; 
      } 
      break; 

     } 
     case SectionDetail: { 

      // Description 
      NSString* htmlString = _description; 
      [_descriptionWebView loadHTMLString:htmlString baseURL:nil]; 
      [[cell contentView] addSubview:_descriptionWebView]; 
      _descriptionWebView.scrollView.scrollEnabled = NO; 
      _descriptionWebView.scalesPageToFit = YES; 


     } 
    } 

return cell; 

}

這裏是我真正遇到問題的一部分。我回國的兩個不同高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.section == SectionHeaderTitle) { 

    // Regular 
    return 54; 

} else { 

    NSInteger height = [[_descriptionWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] integerValue]; 

    return height; 
} 
} 

我找到了另一篇文章,似乎有希望......但是,原諒我,因爲我是新來這個,我不能讓過去它扔我一個錯誤。

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
CGRect frame = webView.frame; 
frame.size = [webView sizeThatFits:CGSizeZero]; 
frame.size.height += 20.0f; // additional padding to push it off the bottom edge 
webView.frame = frame; 

webView.delegate = nil; 

UITableViewCell *cell =[_cells objectAtIndex:webView.tag]; 
[cell.contentView addSubview:[_loadingWebView autorelease]]; 
cell.contentView.frame = frame; 
[cell setNeedsLayout]; 

webView.alpha = 1.0f; 

[self.tableView beginUpdates]; 
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:webView.tag]]; 
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 

// Storing the currently loading webView in case I need to clean it up 
// before it finishes loading. 
_loadingWebView = nil; 
} 

具體UITableViewCell *cell =[_cells objectAtIndex:webView.tag];是給我「爲‘UITableViewCell的’不可見@interface聲明選擇「objectAtIndex。

誰能爲我提供一些指導?我一直停留在這個問題上的天現在。

回答

1

你不應該將UITableViewCells存儲在_cells數組中,單元格可以被tableview重用,因此不會有像indexPaths那樣多的單元格。要獲得正確的單元格實例,你可以問tableView:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath] 

這隻會返回一個單元格,如果它可見。一個更好的解決辦法是做一個

[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone] 
[self.tableView endUpdates]; 

當web視圖加載完成,讓

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

處理單元的佈局。

0

請使用此代碼來獲得的UITableViewCell

UITableViewCell *cell=[tbl_showing_Details cellForRowAtIndexPath:[NSIndexPath indexPathForRow:clickedTag inSection:section]]; 

其中tbl_showing_Details是你的表名,clickedTag是你的當前部分的行和部分就是你的當前部分。

謝謝。