Problems with sizeToFit method of UIWebView

Discussion in 'iOS Programming' started by cstromme, Aug 20, 2009.

  1. cstromme macrumors regular

    Joined:
    Feb 26, 2007
    #1
    Creating an app here to browse a specific web forum, and I'm having some trouble with some UIWebViews.

    I've been trying to add UIWebViews to UITableViewCells for days, but have had problems with getting the height of hte webviews since UITableView checks the height of the cells before adding the cells. Then I realized all I had to do was to create the UIWebViews earlier, put them in an array, wait till they were all loaded and then reload the tableview. That way I could check their height before they were loaded in the cell.

    Now, this seems to work wonders for everything but one. I can't seem to adjust the size of the UIWebViews with sizeToFit!

    This is the method where I previous reloaded the table (where my object is told that the data has been retrieved by the helper object):

    Code:
    - (void)dataReceived
    {
    posts = fetch.posts;
    NSLog(@"Data received!");
         [self createWebViews];
    }
    
    So I call createWebViews:

    Code:
    - (void)createWebViews
    {
         for(NSDictionary *postDict in posts)
         {
              NSString *webString = [postDict objectForKey:@"postContent"];
              CGRect webViewFrame = CGRectMake(0, 0, 280, 1);
              UIWebView *cellWebView = [[UIWebView alloc] initWithFrame:webViewFrame];
              [cellWebView setDelegate:self];
              [cellWebView loadHTMLString:webString baseURL:nil];
              [postViews addObject:cellWebView];
              [cellWebView release];
         }
    }
    I set my class to be a UIWebViewDelegate, and I use these two methods to count how many of the views have finished loading, and then I call reload on the table when they're all done.

    Code:
    - (void) webViewDidFinishLoad:(UIWebView *)sender {
         postViewsLoaded++;
         if(postViewsLoaded == [posts count])
         {
              [self performSelector:@selector(calculateWebViewSize) withObject:nil afterDelay:0.1];
         }
    }
    
    - (void)calculateWebViewSize
    {
         for(UIWebView *postView in postViews)
         {
              NSLog(@"Height b: %f", postView.frame.size.height);
              [postView sizeToFit];
              NSLog(@"Height a: %f", postView.frame.size.height);
         }
         [self hideLoading];
         [self.tableView reloadData];
    }
    Then when reloading I just pull the UIWebView out of postViews and add it to the cell. And in heightForRowAtIndexPath I get the height of the specific UIWebView.


    Everything here works marvelously except for scaling the height of the UIWebView with the sizeToFit method. The two NSLog statements I use show me that the UIWebView's height is 1 both before I call that method and after. Now, if I reload the tableview each time a view has loaded then it seems to be able to scale some of them. This make no sense to me at all.

    Anybody here that have any idea what might be wrong here?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    If you're going to have web views that may have large content inside a tableview I recommend that you make the height of the cells equal to the hight of the screen. The web view scrolls so there's not much point in having a web view that's taller than the screen.

    IMO.
     
  3. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #3
    That aside, I still need to adjust the height.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    So, you're trying to set the height of the UIWebViews so that they don't scroll?

    If not, I'm just not sure how your application is going to differentiate between scrolling a webview that's in a table cell vs. scrolling the table itself.
     
  5. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #5
    The UIWebView won't scroll when embedded in the UITableView at all, the UITableView catches all the the touches up and down. But that's not the issue.

    I don't want scrolling in the views, I just want the UIWebView's height to be auto adjusted based on the context within it, something that is supposed to be possible with the sizetofit.
     
  6. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #6
    Okay, so I came to the conclusion that I could always just use stringByEvaluatingJavaScriptFromString: to evaluate the height and set it manually on the UIWebView, but somehow my UIWebView won't change height.

    This is what I'm doing:

    Code:
    [postView sizeThatFits:CGSizeMake(280,[[postView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight + document.body.offsetTop;"] floatValue])];
    
    EDIT: Solved it. Just needed to set the frame to a new CGRect.

    Now, is there any way I can fire my own method when someone clicks a link in my UIWebView? And how would I even register clicks in it as the UITableView seems to be catching them (or so I assume).
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Ay, there's the rub. I think it may be time to rethink your UI. A tableView with webViews in each cell just doesn't sound practical. Maybe you could consider a tableView with some kind of summary text in each cell (the HTML-page's title, at least, and, perhaps, an image as well). Then when the user selects a row in the table, you push a viewController that is your webView.
     
  8. calvinbhai macrumors newbie

    Joined:
    Jun 23, 2011
    #8
    Here's what worked for me

    I have been in a similar situation.

    Here's what I did:

    Instead of a tableView, used a scrollView, added the webviews as required, and then placed an invisible button on top of the webviews (as a subview of the scrollview). The buttons are tagged. From here on, you know how it'll work.

    Hope this saves some headaches for people trying to use a tabelView in such situations.
     

Share This Page