- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现 NSActionCell
的子类(在 NSTableView
内),并注意到一些不寻常的事情。如果我在用户单击单元格时设置属性 (isEditing
),则该属性的值会丢失,因为 NSCell
此后不久就会被释放。我认为发生这种情况是因为我没有正确处理复制,所以我添加了 copyWithZone
。现在我看到 copyWithZone
被调用 - 但它是在意外实例上调用的 - 并且该实例上的属性是 NO
- 默认值。每次调用 copyWithZone
时,都会在同一个实例上调用它。
有人能解释一下这种行为吗?我附加了有问题的子类以及我得到的输出。当用户单击不同的单元格时,我到底需要做什么才能保留单元格的属性?
@interface MyCell : NSActionCell <NSCoding, NSCopying>
{
}
@property (nonatomic, assign) BOOL isEditing;
@end
@implementation MyCell
- (id)init
{
if ((self = [super init]))
{
[self initializeCell];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self initializeCell];
self.isEditing = [[aDecoder decodeObjectForKey:@"isEditing"] boolValue];
NSLog(@"initWithCoder %ld %i", (NSInteger)self, self.isEditing);
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder: aCoder];
NSLog(@"encode %i", self.isEditing);
[aCoder encodeObject:[NSNumber numberWithBool:self.isEditing] forKey:@"isEditing"];
}
- (void)dealloc
{
NSLog(@"dealloc %ld %i", (NSInteger)self, self.isEditing);
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
MyCell *copy;
if ((copy = [[MyCell allocWithZone:zone] init]))
{
copy.isEditing = self.isEditing;
}
NSLog(@"copy %ld %i new: %ld", (NSInteger)self, self.isEditing, (NSInteger)copy);
return copy;
}
- (void)initializeCell
{
self.isEditing = NO;
}
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
{
return YES;
}
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
{
if (flag)
{
self.isEditing = YES;
NSLog(@"stopTracking %ld %i", (NSInteger)self, self.isEditing);
}
}
@end
输出(当用户单击单元格时生成):
2012-11-21 08:17:59.544 SomeApp[2778:303] copy 4310435936 0 new: 4310152512
2012-11-21 08:18:00.136 SomeApp[2778:303] stopTracking 4310152512 1
2012-11-21 08:18:00.136 SomeApp[2778:303] dealloc 4310152512 1
再次点击(在不同的单元格上):
2012-11-21 08:19:24.994 SomeApp[2778:303] copy 4310435936 0 new: 4310372672
2012-11-21 08:19:25.114 SomeApp[2778:303] stopTracking 4310372672 1
2012-11-21 08:19:25.114 SomeApp[2778:303] dealloc 4310372672 1
最佳答案
听起来您想保留这些属性 - 是吗?
如果您通过将单元格属性存储在模型对象而不是 NSCell 中,并让单元格或 TableView 委托(delegate)从模型中获取值来调整设计,您可能会更轻松。
您试图使用此属性实现什么特定行为?
关于objective-c - NSCell 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497616/
我正在尝试子类化 NSCell 来绘制自定义背景突出显示。该文档似乎建议重写 highlight:withFrame:inView: 应该允许我执行此操作,但该方法永远不会被调用。 相反,我重写了 d
我的 NSTableView 有可变高度的行。完成单元格编辑后以及调整列/表大小期间/之后,行会正确调整大小。当用户在文本单元格中键入内容时,行也会正确调整大小。 但是,在编辑期间单元格本身不会调整大
我正在实现 NSActionCell 的子类(在 NSTableView 内),并注意到一些不寻常的事情。如果我在用户单击单元格时设置属性 (isEditing),则该属性的值会丢失,因为 NSCel
我正在尝试为 NSOutlineView 中的某些单元格设置动画。我需要制作一个图标动画,以作为左侧面板上设备旁边的 iTunes 同步图标进行旋转。 我在动画领域并没有真正的经验,我通过浏览互联网找
过去几天我一直在阅读和尝试在自定义绘制的 NSCell 中允许链接,但基本上没有任何可用的东西,每种方法总是存在问题。 有人知道有效的方法吗? 我使用 - (void)drawInteriorWith
我正在创建一个 NSCell 子类,它将一些对象直接绘制到 View 上(使用 drawInRect:fromRect:operation:fraction:respectFlipped:hints:
我有一个 NSTableView,当用户滚动 tableView 时,我想跟踪包含它的 NSCell 的位置。 我找不到任何有用的东西。如果有人能引导我走向正确的方向,那就太好了! 编辑: 感谢 @K
无法弄清楚如何处理鼠标在NSCell(或NSTextAttachmentCell)上的移动。这可能吗? 最佳答案 您可以在包含单元格的 View 中添加跟踪区域,并在该 View 中实现 mouseE
当我在 NSTableView 中选择 NSTextFieldCell 时,行为会根据是否选择行而有所不同。 如果选择了该行,则立即选择单元格,并且光标在文本字段内闪烁 如果未选择该行,则单击后唯一的
有没有办法在给定表格中选定单元格的行和列索引的情况下获取 NSCell 对象? 我无法使用 NSTableView 委托(delegate)方法,例如 tableView:willDisplayCel
我有一个 NSCell 子类,我可以在其中进行各种自定义绘图。唯一一次事情似乎完全超出我的控制范围是右键单击单元格以显示关联的上下文菜单时。在显示它的上下文菜单时,表格(或单元格 - 不确定到底是哪个
为什么NSImage在NSTableView的NSCell中绘制翻转。以及如何正确纠正这种行为。我从数据源方法返回图像: - (id)tableView:(NSTableView *)tableVie
我希望 OutlineView 中的 NSCell 显示其所在行的子级数。这就是我的意思: (来源:sourceforge.net) 右边是什么,但适用于所有行。 我将如何首先找出 NSCell 所在
我正在使用自定义 NSCell 编写自定义 NSControl。它是一个控件,因此它必须响应鼠标。我在我的控件上创建了一个 NSTrackingArea,实现了 -mouseEntered:、-mou
我正在使用 NSAttributedString 格式化的数据填充 NSOutlineView。到目前为止,我已经设置了文本字体、大小和颜色的格式。我的问题是,选择行时前景色不会改变。如果您创建一个
在我的 cocoa 应用程序中,我需要一个用于 NSTableView 的自定义 NSCell。这个 NSCell 子类包含一个自定义 NSButtonCell 用于处理点击(以及两个或三个用于文本内
我有一个自定义的 NSCell,其中包含各种元素(图像、各种文本片段),其中一个文本 block 可能包含各种可点击的链接。我有我的 NSAttributedString 正确识别链接并将它们着色为蓝
本质上,这是我在 NSTextFieldCell 子类中的代码: - (void)setStringValue:(NSString *)aString { [super setStringVa
我有一个自定义 NSObjects 的 NSArray。每个对象都有一些属性和一个我想在 GridView 中显示的图像。 NSMatrix 似乎是解决我的问题的一个很好的解决方案,但我在获取要显示的
我有一个 NSTableView,其中包含我的自定义 NSCell 子类 IconCell。 IconCell 包含三个元素:图像、文本和按钮。 这是我的绘图代码的简化版本(closeButton 是
我是一名优秀的程序员,十分优秀!