【问题标题】:Resizing UIWebView inside UITableViewCell based on Content根据内容调整 UITableViewCell 内的 UIWebView 大小
【发布时间】:2012-11-11 21:46:57
【问题描述】:

我知道这已经被问死了,我向你保证,我已经阅读了我能找到的关于这个主题的所有帖子。到目前为止,没有什么对我有用。这是我到目前为止所拥有的......我知道 2000 的高度有点荒谬,但我试图在 webView 中查看整个帖子。

- (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。

谁能给我一些指导?我已经被这个问题困扰了好几天了。感谢您的阅读,即使您无能为力。

【问题讨论】:

    标签: objective-c ios uitableview uiwebview


    【解决方案1】:

    您不应将 UITableViewCells 存储在 _cells 数组中。单元格被表格视图重用,因此单元格的数量不会像 indexPaths 那样多。如果您想获得正确的单元格实例,您可以向 tableView 索取:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
    

    这只会返回一个可见的单元格。更好的解决方案是做一个

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

    当 webview 完成加载并让

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

    处理单元格的布局。

    【讨论】:

      【解决方案2】:

      请使用此代码获取 UITableViewCell

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

      其中 tbl_showing_Details 是您的表格名称,clickedTag 是您单元格当前部分的行,section 是您的当前部分。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 1970-01-01
        • 2014-03-30
        • 2014-04-29
        • 2011-09-14
        相关资源
        最近更新 更多