我知道在很多情況下我不必釋放靜態變量。然而,以下是我的模型代碼:發佈靜態對象
+ (UIImage*)imageForTag
{
static UIImage *imgTag;
if(imgTag == nil)
{
NSString* imageName = [[NSBundle mainBundle]
pathForResource:@"tag" ofType:@"png"];
imgTag = [[[UIImage alloc]
initWithContentsOfFile:imageName] autorelease];
}
return imgTag;
}
,這裏是我的數據表部分
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 0)
{
cell.imageView.image = [DataModel imageForSmtng];
}
else if(indexPath.row == 1)
{
cell.imageView.image = [DataModel imageForTag];
}
return cell;
這將在cell.imageView.image = [DataModel imageForTag]
秒崩潰時由於imageForTag
指向無效的地址。如果我添加保留它不會崩潰。從上面刪除autorelease並忘記引用是錯誤的嗎?
你知道'autorelease'會在將來某個時候向你的對象發送一個'release'消息嗎?這是你想要的嗎? – zneak 2010-08-02 05:47:47
是的,但我一直在想,一旦我賦予細胞所有權,它將保留並保留。我想在某些時候細胞正在放棄所有權... – Pablo 2010-08-02 06:02:44