在UITableViewCell中加载异步映像时挂起

时间:2011-11-13 12:47:47

标签: iphone asynchronous uitableview

我正在编写我的第一个iOS应用程序,并且我正在尝试将图像异步加载到我的UITableViewCells中。我正在将背景中的图像加载到NSMutableArray中,然后在cellForRowAtIndexPath方法中检索它。单元格能够加载图像,但在第5个单元格上崩溃并给出了两个不同的错误之一:

ApplicationCell.m中@synthesize行的SIGABRT,对象0x6c30e60的malloc: * 错误:双倍免费

或只输入gdb并指向iconView.image = newIcon; (线程1)。

我在表视图控制器的@interface中声明了NSMutableArray * imageArray并将其设置为@property,然后@合成它:

@synthesize searchArray;
...
- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    self.imageArray = [[NSMutableArray alloc] initWithCapacity:8]; //<-- Am I doing this correctly?
    for (int i=0; i < 8; i++) {
        [self.imageArray addObject:[UIImage imageNamed:@"empty.png"]];
    }
    ...
}

背景中最多会加载8张图片,这些图片会放在imageArray中:

- (void)requestFinishedImage:(ASIFormDataRequest*)request {
    //image request
    NSData *responseData = [request responseData];
    NSIndexPath *index = [NSIndexPath indexPathForRow:request.tag inSection:0];
    UIImage *image = [[UIImage alloc] initWithData:responseData];
    NSLog(@"requested Image with tag: %d",request.tag);
    [imageArray replaceObjectAtIndex:request.tag withObject:image];
    ApplicationCell *cell = (ApplicationCell *)[self.tableView cellForRowAtIndexPath:index];
    [cell setIcon2:image];
    cell.icon = image;
    [image release];
}

这是cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [self.cellNib instantiateWithOwner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;
    }
    UIImage *image;
    NSUInteger row = [indexPath row];

    [cell setItem2: [[searchArray objectAtIndex:row] objectForKey:@"item"]];
    [cell setPrice2: cell.price];
    image = [imageArray objectAtIndex:row];
    NSLog(@"num elements in imageArray = %d, loading: %d",[imageArray count], row);
    [cell setIcon2: image];
[image release];
    return cell;
}

这是我的ApplicationCell.h和ApplicationCell.m

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface ApplicationCell : UITableViewCell
{

    UIImage *icon;
    NSString *item;
    NSString *price;
    IBOutlet UIImageView *iconView;
    IBOutlet UILabel *itemLabel;
    IBOutlet UILabel *priceLabel;
}
- (void)setIcon2:(UIImage *)newIcon;
- (void)setItem2:(NSString *)newItem;
- (void)setPrice2:(NSString *)newPrice;

@property(retain) UIImage *icon;
@property(retain) NSString *item;
@property(retain) NSString *price;

@end

ApplicationCell.m

#import "ApplicationCell.h"

@implementation ApplicationCell

@synthesize icon, item, price;

- (void)setIcon2:(UIImage *)newIcon
{
    [self setIcon:newIcon];
    iconView.opaque = YES;
    iconView.image = newIcon;
}
- (void)setItem2:(NSString *)newItem
{
    [self setItem:newItem];
    itemLabel.text = newItem;
}

- (void)setPrice2:(NSString *)newPrice
{
    [self setPrice:newPrice];
    priceLabel.text = newPrice;
}


- (void)dealloc
{
    [icon release];
    [item release];
    [price release];

    [iconView release];
    [itemLabel release];
    [priceLabel release];

    [super dealloc];
}

@end

代码中可能存在一些错误,因为这是我第一次尝试iOS应用程序,但请告诉我,以便我可以学习!我一直坚持这个异步加载问题太长了..任何帮助都表示赞赏!

1 个答案:

答案 0 :(得分:2)

[image release]中取消-tableView:cellForRowAtIndexPath:来电。你没有保留它,所以你不需要发布它。如果您使用Xcode的静态分析器,它应该会发现这样的内存错误。