开发者

NSOperation to perform uitableview cell image

As i am new in iPad application development please help me in simple problem.

I want to display images in the tableview from the server. it is displaying correctly.

But when i s开发者_如何学运维croll table upside and come back to again there it will again download image data from server.

Please help me.

EDIT:

tableView:cellForRowAtIndexPath:

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 75)];
[imgView setImage:[UIImage imageNamed:@"strip_normal.png"]];
[imgView setHighlightedImage:[UIImage imageNamed:@"strip_hover.png"]];
[cell addSubview:imgView];
[imgView release];

UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 64, 64)];
imgView1.tag = indexPath.row;
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];
[imgView1 setImage:img];
[cell addSubview:imgView1];
[imgView1 release];

return cell;

Thanks in advance.


Your problem is this line

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];

You are downloading the image every time the table view requests for a cell and believe me this happens quite often as table view doesn't construct the entire view in a single shot. It reuses the cells going off screen to present new visible cells. So as soon as a cell goes off screen and comes back on, the cellForRowAtIndexPath: is called again. So the image gets downloaded once more. You are also downloading the image synchronously which will block the UI as well.

To fix this, you should consider downloading them once at the beginning and save them locally in a temporary location and load them into the memory as necessary. Having all of them in the memory can be costly. Additionally, move your download to the background using performSelectorInBackground:withObject. You will have to send the UIKit updates back to the main thread or else you will experience crashes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜