gpt4 book ai didi

cocoa - 使用 NSButtonCell 实例获取自定义 NSCell 来处理鼠标单击/鼠标事件

转载 作者:行者123 更新时间:2023-12-03 17:26:46 25 4
gpt4 key购买 nike

好吧,我真的被这个问题难住了。我想制作一个复选框NSTextFieldCell 组合在一起。复选框很重要如果鼠标击中框而不是文本,则亮起。我已经完成了或多或少是这样,但问题是接收鼠标事件,因为我连续单击一个复选框,但它们全部变成 NSOnState。我将展示我所做的事情以及我的各种失败的尝试,以便让它发挥作用。

这就是我到目前为止所做的:

标题:

@interface MyCheckboxCellToo : NSTextFieldCell {
NSButtonCell *_checkboxCell;
}

实现:

- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
NSLog(@"%@", NSStringFromPoint(point));

NSRect checkFrame;
NSDivideRect(cellFrame, &checkFrame, &cellFrame, cellFrame.size.height/*3 + [[_checkboxCell image] size].width*/, NSMinXEdge);

if (NSMouseInRect(point, checkFrame, [controlView isFlipped])) {
// the checkbox, or the small region around it, was hit. so let's flip the state
NSCellStateValue checkState = ([_checkboxCell state] == NSOnState) ? NSOffState:NSOnState;
[self setState:checkState];
[_checkboxCell setState:checkState];
[controlView setNeedsDisplay:YES];
return NSCellHitTrackableArea;
}
return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
}

我知道我可能不应该这样做:

   [self setState:checkState];
[_checkboxCell setState:checkState];
[controlView setNeedsDisplay:YES];

在那里...因为结果是每个中的每个复选框都会转到NSOnState。这是因为细胞被重复使用了吗?为什么 ImageAndTextCell 在同一个 tableview 中可以有不同的图像?如何处理鼠标事件?

我已经尝试过:

- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {
NSLog(@"%s %@", _cmd, theEvent);
return [_checkboxCell trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
// return YES;
// return [super trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
}

- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView {
NSLog(@"%s %@", _cmd, NSStringFromPoint(startPoint));
return [super startTrackingAt:startPoint inView:controlView];
}

- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint
inView:(NSView *)controlView {
NSLog(@"%s %@", _cmd, NSStringFromPoint(currentPoint));
return [super continueTracking:lastPoint at:currentPoint
inView:controlView];
}

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint
inView:(NSView *)controlView mouseIsUp:(BOOL)flag {
NSLog(@"%s %d %@", _cmd, flag, NSStringFromPoint(stopPoint));
}

trackMouse:...确实被调用

但是

startTrackingAt:...,继续Tracking:...,和 stopTracking:.... DO当我点击“点击区域”复选框时不会被调用

在trackMouse中:...我已经尝试过

return [_checkboxCell trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];

return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];

似乎都不会导致复选框处理鼠标事件。

如何让该复选框进入 NSOnState?我知道我很漂亮关闭但经过大量文档阅读和谷歌搜索后我还没有成功解决了这个问题。

欢迎提出建议和意见..

<小时/>

好的,这里有更多内容来显示对象的创建和销毁..

- (id)init {
if ((self = [super init])) {
_checkboxCell = [[NSButtonCell alloc] init];
[_checkboxCell setButtonType:NSSwitchButton];
[_checkboxCell setTitle:@""];
[_checkboxCell setTarget:self];
[_checkboxCell setImagePosition:NSImageLeft];
[_checkboxCell setControlSize:NSRegularControlSize];

}
return self;
}

- copyWithZone:(NSZone *)zone {
MyCheckboxCellToo *cell = (MyCheckboxCellToo *)[super copyWithZone:zone];
cell->_checkboxCell = [_checkboxCell copyWithZone:zone];
return cell;
}

- (void)dealloc {
[_checkboxCell release];
[super dealloc];
}

最佳答案

我无法直接回答问题的处理事件在单元格部分,但我可以回答以下部分:

I know I probably shouldn't be doing:

[self setState:checkState];
[_checkboxCell setState:checkState];
[controlView setNeedsDisplay:YES];

in there... because the result is that EVERY checkbox in every goes to NSOnState. Is this because cells are re-used?

它们的重用方式与 UITableView 重用 UITableViewCell 的方式不同。

(我注意到您标记了问题“NSTableViewCell”。AppKit 中不存在这样的类。)

How come the ImageAndTextCell can have different images in the same tableview?

两个问题的答案是相同的。

UITableView 为每一行使用一个单元格,并保留与屏幕上的行数一样多的单元格,而 NSTableView 为每一列使用一个单元格。

每一列都作为一个 NSTableColumn 对象存在,并且每个 NSTableColumn 恰好有一个单元格*。 TableView 将单元格的对象值设置为您为该列行交叉点提供的任何值,然后告诉单元格在其边界内的适当位置进行绘制。对于每一行的该列,它都会使用相同的单元格执行相同的操作。

(这里有异常(exception),因为苹果添加了对特定行使用不同单元格的功能,并且因为他们添加了组行,这些行没有列并使用不同的单元格。不过,基 native 制是相同的。)

很抱歉,我无法帮助您一路找到解决方案(除了我的“将它们分开列”的建议之外),但我希望这至少能在一定程度上帮助您。

*它也有一个标题单元格,但这里不算数。我所说的“恰好一个单元格”是它的数据单元格。

关于cocoa - 使用 NSButtonCell 实例获取自定义 NSCell 来处理鼠标单击/鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com