views:

14

answers:

0

I’m working on an app that uses a UIWebView to render content. This webview is in a UIScrollView which, when the user scrolls near the top (or bottom), will append another webview above(/below) the visible one in the scrollview (i.e. pseudo-infinite scrolling).

I’m computing the offset of various elements in the WebView with jQuery’s .offset() method called in -webViewDidFinishLoad:.

Now here’s my real issue: the first webview (the visible one) works correctly. As soon as the next webview is loaded offscreen (though with a sizable frame), I’m getting values like -2147483227 from .offset().top.

Here’s a simplified version of everything, starting with the jQuery snippet:

function getCoordinates(termID) {
    var coords = [];

    $('.itemsICareAbout').each(function() {
        var $self = $(this);
        var id = this.id;
        var pos = $self.offset();
        var height = $self.height();
        coords.push({
            id: id,
            top: pos.top,
            height: height                                          
        });
    });

    return JSON.stringify(coords);
};

And the Objective-C side:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Size it up. Note: the view must be previously drawn for this to work.
    CGSize sizeThatFits = [webView sizeThatFits:CGSizeZero];
    CGRect frame = webView.frame;
    frame.size = sizeThatFits;
    webView.frame = frame;
    NSLog(@"webview.frame: %@", NSStringFromCGRect(webView.frame));

    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"getCoordinates();"];
    NSLog(@"result: %@", result);
}

With this being the log:

2010-10-15 16:10:36.200 Test[21001:207] webView.frame: {{0, 5227}, {504, 1415}}
2010-10-15 16:10:36.199 Test[21001:207] result: [
    {"id":"element1","top":-2147483598,"height":41},
    {"id":"element2","top":-2147483549,"height":41},
    {"id":"element3","top":-2147483500,"height":41},
    {"id":"element4","top":-2147483451,"height":41},
    …
]

It looks like jQuery's .offset() method is overflowing an int, but I’m not positive. Any thoughts?