我正在异步下载表格中的图像。当表格第一次出现时,不会调用 connecion:didrecieveData 但是当我向上或向下移动时,它会被调用并显示图像。好心的帮助
表视图类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
customiseForVideos *cell =(customiseForVideos *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"customiseForVideos" owner:self options:nil];
cell=tblCell;
}
else{
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
[cell.imageActivity startAnimating];
CGRect frame;
frame.size.width=65; frame.size.height=70;
frame.origin.x=24; frame.origin.y=12;
AsyncImageView* asyncImage = [[[AsyncImageView alloc]
initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSURL* url = [NSURL URLWithString:[[videoCollection objectAtIndex:indexPath.row] videoImageUrl]];
//NSLog(@"%@",url);
[asyncImage loadImageFromURL:url activity:cell.imageActivity];
[cell.contentView addSubview:asyncImage];
if((indexPath.row%2)==0)
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-cell-row.png"]] autorelease];
else{
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue-cell-row.png"]] autorelease];
}
return cell;
}
AsyncImageView 类:
- (void)loadImageFromURL:(NSURL*)url activity:(UIActivityIndicatorView *)cellActivity {
activityOnCell=cellActivity;
activityOnCell.hidden=NO;
if (self.connection!=nil) { [self.connection release]; } //in case we are downloading a 2nd image
if (data!=nil) { [data release]; }
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object
NSLog(@"%@",request);
//TODO error handling, what if connection is nil?
}
//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
[self.connection release];
self.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
UIImageView* 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;
[imageView setNeedsLayout];
[self setNeedsLayout];
activityOnCell.hidden=YES;
[data release]; //don't need this any more, its in the UIImageView now
data=nil;
}