我创建了 NSCell 的子类并重写了 setObjectValue 函数来实现我的需求。除了存在泄漏问题之外,一切都很好。 NSCell 的 setObjectValue 函数似乎不会释放其原始对象。
我给类的对象是一个自定义对象,它符合 NSCopying 协议(protocol)并实现了 copyWithZone 函数,如下所示
- (void)setObjectValue:(id < NSCopying >)object {
// I tried to use [[self objectValue] release]; here but the app crashed
[super setObjectValue:object];
//---- other iniatizlize here ---
}
.
- (id)copyWithZone:(NSZone *)zone {
MapComponent *newCopy = [[MapComponent allocWithZone:zone] initWithType:self.componentType];
newCopy.myImage = self.myImage;
return newCopy;
}
我发现了同样的问题here 。没有答案,但可能更好地描述我的情况。
最佳答案
试试这个:
- (id)copyWithZone:(NSZone *)zone {
MapComponent *newCopy = [[[MapComponent allocWithZone:zone] initWithType:self.componentType] autorelease];
newCopy.myImage = self.myImage;
return newCopy;
}
关于objective-c - NSTableView 中的 cocoa 自定义单元格 : NSCell subclassing leak,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4243490/