我知道在很多情况下我不必释放静态变量。然而,以下是我的模型代码:发布静态对象
+ (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