2011-11-13 123 views
0

我正在编程我的第一个iOS应用程序,我试图将图像异步加载到我的UITableViewCells中。我将背景中的图像加载到NSMutableArray中,然后在cellForRowAtIndexPath方法中检索该NSMutableArray。这些细胞能够加载图像,但崩溃的〜5号电池,并给了我两个不同的错误之一:在UITableViewCell中加载异步图像时挂起

SIGABRT在ApplicationCell.m的@synthesize行使用malloc:*错误对象0x6c30e60:双免费

或只是进入gdb和点在iconView.image = newIcon; (线程1)。

我已经宣布的NSMutableArray * imageArray表视图控制器的@interface,并将其设置为@财产,然后@ synthesize'd它:

@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应用程序,但请让我知道,所以我可以学习!我一直被困在这个异步加载问题太长..任何帮助表示赞赏!

+0

行'cell = tmpCell'看起来可能对我不好。 tmpCell从哪里来?它不会在您放置的任何代码中设置。为什么不使用(或分配)一个标准的UITableViewCell,然后添加一个UIImageView子视图? –

+1

tmpCell是在笔尖加载时设置的属性。这是从笔尖加载单元格的可接受方式。 –

回答

2

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

+0

你不知道我现在有多开心,谢谢你帮助我调试!没想到这是一个简单的错误.. – Cody