我的问题很简短,无法通过搜索找到答案。
我的程序是一个用swift和可可编写的简单mac程序。我使用NSTableView类,并且想使用箭头键在各行中上下导航。这直接起作用。但是现在我希望当选择最后一行并按下向下键时,选择第一行。就像周期性边界条件;)
您能帮我解决这个问题吗?还是告诉我我应该读什么书来解决这个问题?
非常感谢:)
答案 0 :(得分:2)
您可以覆盖keydown NSTableView。完成新选择后,别忘了滚动。
SWIFT:
import Carbon.HIToolbox.Events
class TableView : NSTableView {
override func keyDown(with event: NSEvent) {
if event.characters?.count == 1 {
let character = event.keyCode
switch (character) {
case UInt16(kVK_UpArrow):
if selectedRow == 0 {
selectRowIndexes([numberOfRows - 1], byExtendingSelection: false)
scrollRowToVisible(numberOfRows - 1)
//scrollToEndOfDocument(nil)
} else {
super.keyDown(with: event)
}
break
case UInt16(kVK_DownArrow):
if selectedRow == numberOfRows - 1 {
selectRowIndexes([0], byExtendingSelection: false)
scrollRowToVisible(0)
//scrollToBeginningOfDocument(nil)
} else {
super.keyDown(with: event)
}
default:
super.keyDown(with: event)
break
}
} else {
super.keyDown(with: event)
}
}
}
目标C
#import <Carbon/Carbon.h>
@implementation TableView
- (void)keyDown:(NSEvent *)event
{
if ([[event characters] length] == 1) {
unichar code = [event keyCode];
switch (code)
{
case kVK_UpArrow:
{
if ([self selectedRow] == 0) {
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:[self numberOfRows] - 1] byExtendingSelection:NO];
//[self scrollToEndOfDocument:nil];
[self scrollRowToVisible:[self numberOfRows] - 1];
} else {
[super keyDown:event];
}
break;
}
case kVK_DownArrow:
{
if ([self selectedRow] == [self numberOfRows] - 1) {
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
//[self scrollToBeginningOfDocument:nil];
[self scrollRowToVisible:0];
} else {
[super keyDown:event];
}
break;
}
default:
[super keyDown:event]
break;
}
} else {
[super keyDown:event];
}
}
@end