The problem is that I have a nav controller which loads a table. Clicking on a cell pushes another table onto the navigator. Cicking on a cell in that table opens a detail view. Each of these has an image.
To keep the program from hanging, I made the image load asynchronously (http://www.markj.net/iphone-asynchronous-table-image/). This code creates a view with an image, but the image is initialized when the data is added, and so I just add the outer view to my cells.
Now, the trouble. When I load the first table, it never has any images. Sub-tables and detail views show them fine, but when I back up, the image in the cell whose details I was viewing disappears.
Now, code. Sorry that there's a lot of it, but I don't know how to make it more succinct.
I modified AsyncImageView a bit:
A method in class Category. It holds data for the second table:
The first table's cellForRowAtIndexPath:
(You might notice that my memory management is atrocious. I'm still mystified by it, so I just retain everything just in case.)
To keep the program from hanging, I made the image load asynchronously (http://www.markj.net/iphone-asynchronous-table-image/). This code creates a view with an image, but the image is initialized when the data is added, and so I just add the outer view to my cells.
Now, the trouble. When I load the first table, it never has any images. Sub-tables and detail views show them fine, but when I back up, the image in the cell whose details I was viewing disappears.
Now, code. Sorry that there's a lot of it, but I don't know how to make it more succinct.
I modified AsyncImageView a bit:
Code:
//
// AsyncImageView.h
// Postcard
//
// Created by markj on 2/18/09.
// Copyright 2009 Mark Johnson. You have permission to copy parts of this code into your own projects for any use.
// www.markj.net
//
@interface AsyncImageView : UIView
{
//could instead be a subclass of UIImageView instead of UIView, depending on what other features you want to
// to build into this class?
NSURLConnection* connection; //keep a reference to the connection so we can cancel download in dealloc
NSMutableData* data; //keep reference to the data so we can collect it as it downloads
//but where is the UIImage reference? We keep it in self.subviews - no need to re-code what we have in the parent class
UIImageView* imageView;
}
-(void) setFrame:(CGRect) rect;
@end
Code:
#import "AsyncImageView.h"
// This class demonstrates how the URL loading system can be used to make a UIView subclass
// that can download and display an image asynchronously so that the app doesn't block or freeze
// while the image is downloading. It works fine in a UITableView or other cases where there
// are multiple images being downloaded and displayed all at the same time.
@implementation AsyncImageView
- (id)initWithURL:(NSString*)urlAddr
{
self = [super initWithFrame:CGRectZero];
NSURL* url = [NSURL URLWithString:urlAddr];
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection retain];
[self retain];
return self;
}
//the URL connection calls this repeatedly as data arrives
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData
{
if (data==nil)
{
data = [[NSMutableData alloc] initWithCapacity:2048];
}
[data appendData:incrementalData];
}
//the URL connection calls this once all the data has downloaded
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
//so self data now has the complete image
[connection release];
connection=nil;
if ([[self subviews] count] > 0)
{
//then this must be another image, the old one is still in subviews
[[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
}
//make an image view for the image
imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
[self addSubview:imageView];
imageView.frame = self.bounds;
UIView* v = imageView;
while (v != nil)
{
[v setNeedsLayout];
v = v.superview;
}
[data release]; //don't need this any more, its in the UIImageView now
data=nil;
}
-(void) setFrame:(CGRect) rect
{
[super setFrame:rect];
imageView.frame = rect;
}
//just in case you want to get the image directly, here it is in subviews
- (UIImage*) image
{
UIImageView* iv = [[self subviews] objectAtIndex:0];
return [iv image];
}
- (void)dealloc
{
[connection cancel]; //in case the URL is still downloading
[connection release];
[data release];
[super dealloc];
}
@end
A method in class Category. It holds data for the second table:
Code:
-(id* ) initWithName:(NSString* )nm andImg:(NSString* )imgU
{
name = nm;
[name retain];
if (imgU != nil)
{
img = [[AsyncImageView alloc] initWithURL:imgU];
[img retain];
}
data = [NSMutableArray arrayWithCapacity:0];
[data retain];
return self;
}
The first table's cellForRowAtIndexPath:
Code:
- (UITableViewCell* )tableView:(UITableView* )tableView cellForRowAtIndexPath:(NSIndexPath* )indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
}
Category* c = [xmlList objectAtIndex:indexPath.row];
c.img.tag = 999;
c.img.frame = CGRectMake(0, 5, 90, 90);
[[cell.contentView viewWithTag:999] removeFromSuperview];
[cell addSubview:c.img];
UILabel* textLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, 25, 0, 0)];
[textLabel retain];
textLabel.text = c.name;
textLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
[textLabel sizeToFit];
[cell addSubview:textLabel];
[cell retain];
return cell;
}
(You might notice that my memory management is atrocious. I'm still mystified by it, so I just retain everything just in case.)