objective-c - NSTableView 中的 cocoa 自定义单元格 : NSCell subclassing leak

标签 objective-c cocoa nstableview nscell

我创建了 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/

相关文章:

objective-c - 计划任务以在 iOS 中执行方法

ios - 将 kAudioUnitSubType_MultiChannelMixer 添加到 AUGraph 时静音

objective-c - 计算文本容器的高度

objective-c - 在 NSTextView 中捕获撤消和重做编辑文本组

objective-c - 如何将数据重新加载到 NSTableView 中?

cocoa - Core Data 和 NSArrayController 的行为非常奇怪

iphone - 从 UIViewController 访问 UIView

iOS - 在运行 iOS7 的旧设备上模糊和错误的视网膜图形

swift - 如何在Swift中学习 'tableViewSelectionDidChange:'里面的NSTableView中的selection确实发生了变化?

ios - UITableViewCell 只接受长按