UITableViewCell holding a UIWebViewCell with Dynamic Height and a Loading Animation - iphone

I am currently trying to nest a UIWebView (among other elements) into a custom UITableViewCell subclass.
I need to dynamically determine the size of the cell (using heightForRowAtIndexPath) but for some reason when I do it like so, the height always gets printed as 0:
UIWebView* wv = [[[UIWebView alloc] init] autorelease];
[wv loadHTMLString:body baseURL:nil];
[wv setNeedsLayout]; //I don't think these are necessary but I tried anyway.
[wv setNeedsDisplay];
CGSize wvSize = [wv sizeThatFits:webViewBounds];
NSLog(#"WVHEIGHT %f", wvSize.height); //THIS IS WHERE IT PRINTS.
CGFloat retVal = 10.0f;
retVal += 50 > wvSize.height ? 50 : wvSize.height;
retVal += 2 + 15 + 10;
return retVal;
Now, if I do that exact same calculation using the cell.webView that I have access to in cellForRowAtIndexPath, it returns a non-zero value. However, I'd really like to avoid loading an entire cell just to figure out how tall it should be...
Additionally, UIWebView is very slow at rendering text (and it is unfortunately not negotiable that I use UIWebView), so I was wondering if there's a way to tell it to display the typical Apple "rotating gear" activity icon until it has its text fully loaded and ready to render.

I would recommend hiding each UIWebView until it is done loading, and showing a loading indicator instead. I would then make a dictionary with keys that match the tags, and for each entry in the dictionary, I would store the size of the UIWebView.
Make your controller conform to the UIWebViewDelegate protocol, and use the following code:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('this').offsetHeight;"];
[self.webViewHeights setValue:[output floatValue] forKey:[NSString stringWithFormat:#"%f", webView.tag]];
}
After that, add code to hide your loading view and display your UIWebView, then call a table reload. In your cell height method, just check the correct array element for your UIWebView height.
Edit:
It seems I left out the important part!
OK, so, to create non-visible UIWebViews, you just need to create them, but not actually add them to a view. For example, you could do something like this in your viewDidLoad method:
UIWebView *newWebView = [[UIWebView alloc] initWithFrame:newFrame];
[newWebView loadHTMLString:body baseURL:nil];
newWebView.tag = 1;
[self.webViewDictionary setObject:newWebView forKey:#"1"];
Then in your table code, say something like
NSString *tagString = [NSString stringWithFormat:#"%d", indexPath.row];
UIWebView *curWebView = [self.webViewDictionary objectForKey:tagString];
if (curWebView.loading == NO) {
CGRect newFrame = curWebView.frame;
newFrame.size.height = [self.webViewHeights objectForKey:tagString];
curWebView.frame = newFrame;
[cell addSubview:curWebView];
} else {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorStyleGray];
[cell addSubview:activityIndicator];
}

I don't think sizeThatFits: is what you want to use.
sizeThatFits:
Asks the view to calculate and return
the size that best fits its subviews.
Your webview does not have any subviews, only HTML.
UIWebView has no init method of its own, so you probably should use initWithFrame: (from the UIView superclass) to set the size directly. You can set it to the size you want, which is the size of the tableView cell in this case.

Related

Display JSON as List in ViewController (Not in TableView)

I am producing a JSON string that I need to parse out and display onto the page. My JSON string outputs information about the contents of a CD like this:
[{"song_name":"Song 1","artist":"John","price":"$1"},
{"song_name":"Song 2","artist":"Anna","price":"$2"},
{"song_name":"Song 3","artist":"Ryan","price":"$3"}]
I would like to display the contents in my viewController in a list format displaying the song_name, artist, and price. I do not want to use a tableView to display this information, but would rather just have a list displayed. How might I go about doing this? I would assume that I need to use NSJSONSerialization, but have only used that for a tableView in the past. Thank you!
In addition to other answers, you can use only one label, just create NSMutableString (for dynamicly adding tracks info) with #"\n" between tracks info, pass it to label.text and set UILabel's property numberOfLines to 0
Follow these steps:
Parse the JSON and store the key-value pair(NSDictionary of CDs) in an NSArray (say infoArray)
Add a UIScrollview as a subview on your viewController's view.
Allocate UILabels dynamically, depending on infoArray count. Looking at your data I believe you can initialize labels with static frames i.e your y can remain static.
Add the text from the infoArray on this label.
Still, I would suggest use UITableView only. It is much simpler and a better approach
You make an array of dictionaries using NSJSONSerialization indeed, then you parse this array one by one and create a view of every dictionary. You're probably best of using a method for this:
-(UIView *) listView: (NSString *)songName andArtist:(NSString *)artist andPrice:(NSString *)price andIndex:(int)viewIndex {
//create your view here and do anything you want
UIView *subView = [[UIView alloc] init] autoRelease];
subView.frame = CGRectMake(0, viewIndex * 70, self.view.frame.width, 70);
//add labels and other stuff
// return the view
return subView;
}
The you add it to the current view by setting different Y values so they appear underneath each other by using the viewIndex of the former method... So if you have an array it goes something like this:
for (int i = 0; i < [array count]; i++) {
NSDictionary *dict = [array objectAtIndex:i];
NSString *songName = [dict valueForKey:#"song_name"];
NSString *artist = [dict valueForKey:#"artist"];
NSString *price = [dict valueForKey:#"price"];
UIView *tempView = [self listView:songName andArtist:artist andPrice:price andIndex:i];
[self.view addSubView:tempView];
}
You have to add it all to a scrollview otherwise you will run into the problem of to many rows on the page. Google for UIScrollView if you don't know how.
But I would recommend against this approach.. Tableviews are there with a reason. They are made for this stuff. Because the also provide for scrolling, drawing and refreshing. If you can, use them!

Memory problems with UIWebViews

I have a webviewcontroller, which I create one instance of, and then I change the request, and show the webview based on which row is selected in a tableview.
// Replace tableView:didSelectRowAtIndexPath with the following
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
CGRect rect = self.view.frame;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
rect.size.width = width; //self.view.frame.size.width;
rect.size.height = height; //self.view.frame.size.height;
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
page = [[WebViewController alloc] initWithUrl:entry.articleUrl];
page.view.frame = rect;
[self.view addSubview:page.view];
}
My problem is, that after loading a few of these, I get memory warnings and crash.
In another tab, I also have a webview which loads a google map when the tab loads. This crashes the app almost as soon as it is loaded.
Since I am only making one instance of each, I don't see why this problem is happening. Previously I created a new webviewcontroller each time, and then released it, but I had the same problem.
Here is the code to load the Google map:
#interface MapViewController : UIViewController {
IBOutlet UIWebView *mapPage;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
url_string = #"http://maps.google.fr/maps/ms?msa=0&msid=213119412551786463007.0004b3fb7f4c123377402&z=12";
url = [NSURL URLWithString:url_string];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_string]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
mapPage.scalesPageToFit = YES;
mapPage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[mapPage loadRequest:request];
[super viewDidLoad];
}
The UIWebView is created in interface builder.
It runs out of memory and crashes right after the map is finished loading, or as soon as you try to interact with the map. It appears to be the very same problem as above, but this time there are no tabs, or no changing the request that the UIWebView uses.
Each time when you select row in UITableView you create a new object of WebViewController and add its view (for some reason?) to current controller's view. I'm not sure that you at least remove that view from superview (you also have to release WebViewController). And of course it will take more and more memory.
And what is the reason to create a whole UIViewController and use it as subview? Why you didn't use UINavigationController?
It turns out the pages were being cached.
In the applicationDidReceiveMemoryWarning method of my application, I added a line to clear the cache. This fixed both of my p
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}

Set accessibility badge on a tabbarItem

I have a tabbar application. If there is a badge on a certain tab VoiceOver pronounces N items. I'd like to make it pronounce N messages.
How do I do that?
I had a similar problem,
it seems that UITabBarViewController.tabbar is handling accessibility differently than other views.
So my solution was placing another uiview above the badge as a subview of [tabbar superview] and adding accessibility to this view:
At viewDidLoad:
self.badgeAccessibilityView = [[UIView alloc] init];
//use tabBarView.frame to calculate
self.badgeAccessibilityView.frame = frameAccordingToBadgeLocation;
self.badgeAccessibilityView.userInteractionEnabled = YES;
self.badgeAccessibilityView.isAccessibilityElement = YES;
[self.tabBarView.superview addSubview:self.badgeAccessibilityView];
On badge value change:
self.badgeAccessibilityView.accessibilityLabel =
[NSString stringWithFormat:#"%d Notifications", badgeValue];

SSCollectionView SSCollectionViewItem - no Items displayed

I ran into difficulties with SSCollectionView and SSCollectionViewItem.
First of all I'd like to get it initialized from IB. But that won't work for me.
I have a SelectFooViewController which is:
#interface SelectFooViewController : SSCollectionViewController { ... }
and am using it as filesOwner of the corresponding XIB.
SelectFooViewController* selectFooVC = [[SelectFooViewController alloc]
initWithNibName:#"SelectFooViewController" bundle:nil];
But since it wont work I had to initialize its properties inside viewDidLoad() myself.
Furthermore I am not able to display anything except the backgroundColor of my SSCollectionViewItems. What I want is a textLabel and an image .
- (SSCollectionViewItem *)collectionView:(SSCollectionView *)aCollectionView itemForIndexPath:(NSIndexPath *)indexPath {
SSCollectionViewItem *item = [[[SSCollectionViewItem alloc] initWithStyle:SSCollectionViewItemStyleImage reuseIdentifier:itemIdentifier] autorelease];
SSLabel* label = [[SSLabel alloc] init];
[label setText:#"foo"];
item.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]];
item.textLabel = label;
[label autorelease];
return item;
}
I can confirm that the delegate methods (for determining the number Of rows, sections and such) are implemented and working as expected. But my items are all empty - but react onclick with the expected popup.
Does anyone see an error in what I did? - Thanks...
EDIT: I was also not able to display a local image by changing SSCatalog project
I just figured out, that I have to set the frame of each property (textLabel, detailTextLabel and imageView) myself. That fixed it.
When you create instance SelectFooViewController just insert this line
selectFooVC.view;
or
selectFooVC.view.hidden = NO;
And then add it to the view.
This is because the view is not initalised until you explicitly access it. Hence your items are loaded only when you click it and not immediately. You can call it a hack but i don't call it one. :-)

Views load slowly when switching between views on 3G iPhones - how to change my style?

In my iPhone app, views will often load slowly when transitioning, like if a user clicks a button on the Tab Bar Controller. This happens more if the phone is low on memory. It doesn't really come up on 3GS phones, but it's a big problem on 3G phones.
I suspect that I'm not following some best practices for creating UIViewControllers. I think I might be doing too much in the init functions, not using the viewDidLoad function, or something. It seems to affect all my views, so I think it's a problem with my style in general, not some particular snippet.
Can anyone tell me what i might be doing wrong? Here is some sample code, from a UIViewController subclass:
EDIT: In response to the question: "where is this being called?"
This function gets called in this case when the user clicks a marker on the map:
if(marker.label.tag == SavedBookmarkTag) {
SavedDetailScreen *savedBookmark = [[[SavedDetailScreen alloc] initBookmarkView:
[(NSDictionary *)marker.data objectForKey:#"bookmark"]]autorelease];
[savedBookmark showMap];
[self.navBar pushViewControllerWithBackBar:savedBookmark];
return;
}
END EDIT
-(id)initBookmarkView: (Bookmark *)bm {
self = [self initView];
self.bookmark = bm;
primaryLabel.text = [bm title];
secondaryLabel.text = [self getLineWithLat:[bm lat] AndLon:[bm lon] AndDate:[bm timeCreated]];
return self;
}
- (id)initView {
self = [super init];
self.isWaypoint = NO;
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"238-beveled-background.png"]];
bg.frame = CGRectMake(0, 0, 320, 376);
[self.view addSubview:bg];
bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"small-label.png"]];
[self.view addSubview:bg];
[bg release];
self.primaryLabel = [[UILabel alloc]init];
primaryLabel.font = TITLE_FONT;
primaryLabel.backgroundColor = [UIColor clearColor];
primaryLabel.textColor = LIGHT_BLUE;
self.secondaryLabel = [[UILabel alloc]init];
secondaryLabel.font = TEXT_FONT;
secondaryLabel.backgroundColor = [UIColor clearColor];
secondaryLabel.textColor = LIGHT_BLUE;
secondaryLabel.lineBreakMode = UILineBreakModeClip;
self.thirdLabel = [[UILabel alloc]init];
thirdLabel.font = TEXT_FONT;
thirdLabel.backgroundColor = [UIColor clearColor];
thirdLabel.textColor = LIGHT_BLUE;
thirdLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[self.view addSubview:primaryLabel];
[self.view addSubview:secondaryLabel];
[self.view addSubview:thirdLabel];
self.loadingBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"stats-box.png"]];
loadingBackground.frame = CGRectMake(0, 115, loadingBackground.frame.size.width, loadingBackground.frame.size.height);
[self.view addSubview:loadingBackground];
[self.view sendSubviewToBack:loadingBackground];
AnimatedGif *animatedGif = [[[AnimatedGif alloc] init] autorelease];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"35" ofType:#"gif"]];
[animatedGif decodeGIF: data];
UIImageView *loadingImage = [animatedGif getAnimation];
loadingImage.frame = CGRectMake(150,150,loadingImage.frame.size.width,loadingImage.frame.size.height);
[loadingImage startAnimating];
[loadingBackground addSubview:loadingImage];
[loadingImage release];
[self layoutSubviews];
return self;
}
- (void) layoutSubviews {
self.view.frame = CGRectMake(0,0,320,372);
primaryLabel.frame = CGRectMake(30, 30, 260, 18);
secondaryLabel.frame = CGRectMake(30 ,52, 260, 16);
thirdLabel.frame = CGRectMake(30, 72, 260, 16);
}
The slowness that you're seeing can probably be attributed to the fact that constructing objects and reading data from the flash are both expensive processes. Look for opportunities to reuse existing objects rather than constructing them multiple times, and consider deferring especially expensive operations until after the view gets displayed.
In this case, I would start with a couple changes:
Make savedBookmark a member variable so that you can construct it once and reuse it. Replace your initBookmarkView: method with a setBookmarkView: method that you can call after this member variable is constructed to reconfigure your labels for the specific bookmark being displayed.
Take the subview creation code out of initView and put it in loadView. This is the most appropriate place to construct your own view hierarchy programmatically. UIViewController implements lazy loading on its view property to defer construction as long as possible. The view property is nil until the first time it's requested. At that point UIViewController calls loadView to set the property. The default implementation loads the view from a nib file if one is defined. Otherwise it just constructs an empty UIView and makes that the main view. Note that you'll have to construct the container view and set the view property yourself.
In other apps you may get some improvement by moving some initialization code into viewDidLoad:, which gets called after the view property is loaded, whether programmatically or from a nib. If you ever have an especially slow operation like loading images from a remote URL, you might want to start loading the data asynchronously in viewDidLoad:, and then update your subviews once the data finishes loading. In some cases you may also want to defer some code until viewDidAppear:. Just be aware that this method gets called every time the view appears, unlike loadView and viewDidLoad:, which only get called once.