Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing UIWebView inside UITableViewCell based on Content

I know this has been asked to death, and I assure you I have read through every thread I could find on this topic. Nothing has worked for me so far. Here's what I have so far... I understand the height of 2000 is a little ridiculous, but I'm trying to see the whole post in the 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;

}

Here's the part I'm really having trouble with. I'm returning two different heights

- (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;
}
}

I've found another post that seemed promising... However, and forgive me because I'm new to this, I can't get past an error it's throwing me.

- (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;
}

Specifically UITableViewCell *cell =[_cells objectAtIndex:webView.tag]; is giving me "No visible @interface for 'UITableViewCell' declares the selector 'objectAtIndex.

Can anyone provide me with some guidance? I've been stuck on this issue for days now. Thanks for reading, even if you can't help.

like image 214
Miles Avatar asked Mar 23 '26 21:03

Miles


1 Answers

You should not store UITableViewCells in a _cells array. The cells are reused by the tableview so there will not be as many cells as there are indexPaths. If you want to get the right cell instance you can ask the tableView for it:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]

This will only return a cell if its visible. A better solution would be to do a

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

when the webview is done loading and let the

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

handle the layout of the cell.

like image 188
Jorn van Dijk Avatar answered Mar 26 '26 11:03

Jorn van Dijk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!