私のiPhoneアプリケーションには、カスタムimageviewを備えたテーブルビューがあり、AsyncImageViewクラスを使用してリモートロケーションから画像をロードしています。うまく機能しますが、1つの問題は、テーブルをスクロールすると、セルがデキューされ、サーバーから画像を取得しようとすることです。そのため、AsyncImageViewクラスから画像を読み込むためのメソッドが何度も呼び出されるため、メモリ割り当てが増加し、最終的にアプリがクラッシュします。
これが私のコードです:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 300, 40);
CGRect userImageFrame = CGRectMake(5, 7, 36, 36);
UIImageView *userImage;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
[cell setFrame:CellFrame];
userImage = [[UIImageView alloc]init];
userImage.frame = userImageFrame;
userImage.tag = 3;
[cell.contentView addSubview:userImage];
[userImage release];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
else
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView];
UIImageView *userImage = (UIImageView *)[cell viewWithTag:3];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSString *url = [[NSString alloc]initWithFormat:@"%@%@", CommonImageURL,[AllUsersProfileImageArray objectAtIndex:indexPath.row]];
NSURL *imageUrl = [NSURL URLWithString:[url stringByAppendingFormat:@"?%i", rand()]];
[url release];
userImage.image = [UIImage imageNamed:@"defaultPerson.png"];
userImage.imageURL = imageUrl;
return cell;
}
問題を解決するための可能な方法はありますか?助けてください。