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):
So I call createWebViews:
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.
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?
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?