NSTableView:检测鼠标点击与行和列 [英] NSTableView: detecting a mouse click together with the row and column
问题描述
我尝试检测在NSTableView中发生鼠标点击的时间,以及当它发生时,确定被点击的单元格的行和列。 >到目前为止,我试图使用NSTableViewSelectionDidChangeNotification,但有两个问题:
- 只有当选择更改时触发,
- 当我的委托被调用时,NSTableView的clickedRow和clickedColumn属性都为-1。
有更好的方法吗?
捕获用户点击某行时(仅当用户点击某行时,而不是以编程方式选择时):
子类化您的NSTableView声明协议
@protocol ExtendedTableViewDelegate< NSObject>
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row;
@end
@interface MyTableView:NSTableView
@property(nonatomic,weak)id< ExtendedTableViewDelegate> extendedDelegate;
@end
MyTableView.m / p>
处理鼠标按下事件(注意,当用户点击外部时,不会调用委托回调,也许您也想处理那么只需注释条件 if(clickedRow!= -1)
)
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];
[super mouseDown:theEvent];
if(clickedRow!= -1){
[self.extendedDelegate tableView:self didClickedRow:clickedRow];
}
}
使您的WC,VC符合ExtendedTableViewDelegate 。
@interface MyViewController:DocumentBaseViewController< ExtendedTableViewDelegate,NSTableViewDelegate,NSTableViewDataSource>
将MyTableView的extendedDelegate设置为WC,VC(MyViewController)
在MyTableView.m
中的某处 self.myTableView.extendedDelegate = self
在委托(MyViewController.m)
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row {
// have fun
}
I'm trying to detect when a mouse click occurs in an NSTableView, and when it does, to determine the row and column of the cell that was clicked.
So far I've tried to use NSTableViewSelectionDidChangeNotification, but there are two problems:
- It only triggers when the selection changes, whereas I want every mouse click, even if it is on the currently selected row.
- The clickedRow and clickedColumn properties of NSTableView are both -1 when my delegate is called.
Is there a better (and correct) way of doing this?
To catch the user clicking a row (only, when the user clicks a row, not when it is selected programmatically) :
Subclass your NSTableView and declare an protocol
MyTableView.h
@protocol ExtendedTableViewDelegate <NSObject>
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row;
@end
@interface MyTableView : NSTableView
@property (nonatomic, weak) id<ExtendedTableViewDelegate> extendedDelegate;
@end
MyTableView.m
Handle the mouse down event (note, the delegate callback is not called when the user clicks outside, maybe you want to handle that too, in that case, just comment out the condition "if (clickedRow != -1)
")
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedRow = [self rowAtPoint:localLocation];
[super mouseDown:theEvent];
if (clickedRow != -1) {
[self.extendedDelegate tableView:self didClickedRow:clickedRow];
}
}
Make your WC, VC conform to ExtendedTableViewDelegate.
@interface MyViewController : DocumentBaseViewController<ExtendedTableViewDelegate, NSTableViewDelegate, NSTableViewDataSource>
set the extendedDelegate of the MyTableView to your WC, VC (MyViewController)
somewhere in MyTableView.m
self.myTableView.extendedDelegate = self
Implement the callback in delegate (MyViewController.m)
- (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row {
// have fun
}
这篇关于NSTableView:检测鼠标点击与行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!