Explicitly set the row height for some rows, but use the estimated RowHeight for others.

I have UITableViewCell

one that contains UIWebView

both subview. The web view fills the entire cell. I am using estimatedRowHeight

to calculate line height. However, at the time the table view is built, the cell has no height because the web view has not loaded its content, so the cell has no content. Because of this, it estimatedRowHeight

returns 44 instead of the correct webview content height.

Does anyone know how I can correctly calculate the line height when the content is not immediately set? Is there a way to estimate the height of some cells and explicitly set the height of other cells in the same table view?

This is how I use estimatedRowHeight

:

self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;

      

I am not using the delegate method. I tried this, but it doesn't change the result. The cell I'm using has an xib that also uses Auto Layout constraints. To be clear, the cell does appear and the web view is actually added to the cell. The problem is that the cell height is not large enough to show the entire webview. WebView downloads the HTML embed code for the audio player.

+3


source to share


2 answers


I played around a bit and found out that you can use UITableViewAutomaticDimension

like this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableSection *tableSection = (self.tableModel.sections)[indexPath.section];

    if (tableSection.sectionType == TableSectionTypeWebView)
    {
        return 120;
    }
    else
    {
        return UITableViewAutomaticDimension;
    }
}

      

This basically says that any section of the WebView uses a height of 120, but for everything else, I want you to define the height. Here I am using my own table of tables (i.e. TableSection, sectionType, etc.)



I had to add self.tableView.estimatedRowHeight = 200;

to my method init

to work.

Now I can provide an approximate line height, but also explicitly set the line height for some sections, or even some lines if I want to.

I haven't seen any documentation for this, but I tested it with variable length strings and it was perfectly supported.

+10


source


You make your class UIWebViewDelegate and then include and set your class as a delegate for each individual UIWebView interface in your UITableViewCell

-(void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    //Asks the view to calculate and return the size that best fits //its subviews.
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;
    [self.tableView beginUpdates];
    [self.tableView  endUpdates];
}

      

Now you can get the height of the UIWebView and set it to the height of the rows, because the next method will be called again after calling 'beginUpdates'



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:  (NSIndexPath*)indexPath {
    return _webView.frame.size.height;
}

      

Hope it helps

0


source







All Articles