我正在編程我的第一個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應用程序,但請讓我知道,所以我可以學習!我一直被困在這個異步加載問題太長..任何幫助表示讚賞!
行'cell = tmpCell'看起來可能對我不好。 tmpCell從哪裏來?它不會在您放置的任何代碼中設置。爲什麼不使用(或分配)一個標準的UITableViewCell,然後添加一個UIImageView子視圖? –
tmpCell是在筆尖加載時設置的屬性。這是從筆尖加載單元格的可接受方式。 –