使用按键从表格视图中删除条目

发布于 2024-12-12 10:06:53 字数 164 浏览 0 评论 0原文

我有一个简单的可可应用程序,其中有一个显示一些条目的表格视图。我希望用户能够通过简单地在表中选择它并按“删除”来删除他想要的任何条目。如何检测“删除”按键以及如何知道它是否在用户选择表视图中的条目时发生?

**编辑:如果我没有说清楚,“删除”指的是 Mac 键盘上的退格键,而不是 GUI 按钮。

I have a simple cocoa application with a table view displaying a few entries. I want the user to be able to delete any entry he wants by simply selecting it in the table and pressing "Delete". How do I detect the keypress of "Delete" and how do I know if it occurs when the user has selected an entry in the tableview?

**Edit: in case I've not made it clear, "Delete" refers to the backspace key on a mac keyboard and not a GUI button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍暮颜 2024-12-19 10:06:53

对表视图进行子类化,并让它在按下删除键时发送其委托事件。

MyTableView.h:

@interface MyTableView : NSTableView

@end

@protocol MyTableViewDelegate <NSTableViewDelegate>

-(BOOL)deleteKeyPressedForTableView:(MyTableView *)tableView;

@end

MyTableView.m:

+(unichar)firstCharPressedForEvent:(NSEvent *)theEvent {
    if (![[theEvent characters] length]) return -1;
    return [[theEvent characters] characterAtIndex:0];
}

+(BOOL)eventIsDeleteKeyPressed:(NSEvent *)theEvent {
    switch ([MyTableView firstCharPressedForEvent:theEvent]) {
        case NSDeleteFunctionKey:
        case NSDeleteCharFunctionKey:
        case NSDeleteCharacter:
            return YES;
        default:
            return NO;
    }
}

-(void)keyDown:(NSEvent *)theEvent {
    if ([MyTableView eventIsDeleteKeyPressed:theEvent])
        if ([[self delegate] respondsToSelector:@selector(deleteKeyPressedForTableView:)])
            if ([(id<MyTableViewDelegate>)[self delegate] deleteKeyPressedForTableView:self])
                return;

    // The delegate wasn't able to handle it
    [super keyDown:theEvent];
}

表格视图的委托:

- (BOOL)deleteKeyPressedForTableView:(MyTableView *)tableView {
    // check if row is selected
    if (no_row_selected) return NO;
    // handle deletion
    if (error_condition) return NO;    
    return YES;
}

Subclass the table view, and have it send its delegate events when delete is pressed.

MyTableView.h:

@interface MyTableView : NSTableView

@end

@protocol MyTableViewDelegate <NSTableViewDelegate>

-(BOOL)deleteKeyPressedForTableView:(MyTableView *)tableView;

@end

MyTableView.m:

+(unichar)firstCharPressedForEvent:(NSEvent *)theEvent {
    if (![[theEvent characters] length]) return -1;
    return [[theEvent characters] characterAtIndex:0];
}

+(BOOL)eventIsDeleteKeyPressed:(NSEvent *)theEvent {
    switch ([MyTableView firstCharPressedForEvent:theEvent]) {
        case NSDeleteFunctionKey:
        case NSDeleteCharFunctionKey:
        case NSDeleteCharacter:
            return YES;
        default:
            return NO;
    }
}

-(void)keyDown:(NSEvent *)theEvent {
    if ([MyTableView eventIsDeleteKeyPressed:theEvent])
        if ([[self delegate] respondsToSelector:@selector(deleteKeyPressedForTableView:)])
            if ([(id<MyTableViewDelegate>)[self delegate] deleteKeyPressedForTableView:self])
                return;

    // The delegate wasn't able to handle it
    [super keyDown:theEvent];
}

Table view's delegate:

- (BOOL)deleteKeyPressedForTableView:(MyTableView *)tableView {
    // check if row is selected
    if (no_row_selected) return NO;
    // handle deletion
    if (error_condition) return NO;    
    return YES;
}
长不大的小祸害 2024-12-19 10:06:53

另一种选择是仅将等效键(键盘快捷键)分配给“编辑”菜单的“删除”项,并将其操作设置为控制器的 delete: 方法。

Another option is to just assign a key equivalent (keyboard shortcut) to the Edit menu's "Delete" item and set its action to your controller's delete: method.

套路撩心 2024-12-19 10:06:53

如果您只有一张表,一种更简单的解决方案是在窗口控制器或视图控制器中实现一些 NSResponder 方法:

- (void)keyDown:(NSEvent *)event
{
    [self interpretKeyEvents:@[event]];
}

- (void)deleteBackward:(id)sender
{
    // use the tableView's selectedRow to determine what/if to delete
}

If you only have one table, a simpler solution is to implement a few NSResponder methods in your window controller or view controller:

- (void)keyDown:(NSEvent *)event
{
    [self interpretKeyEvents:@[event]];
}

- (void)deleteBackward:(id)sender
{
    // use the tableView's selectedRow to determine what/if to delete
}
悲凉≈ 2024-12-19 10:06:53

这是 @paulmelnikow 给出的答案的 Swift 4 版本。

protocol MyTableViewDelegate: NSTableViewDelegate {
    func deleteKeyPressed(for tableView: MyTableView)
}

class MyTableView: NSTableView {
    override func keyDown(with event: NSEvent) {
        guard let myDelegate = delegate as? MyTableViewDelegate, deletePressed(event: event) else {
            super.keyDown(with: event)
            return
        }

        myDelegate.deleteKeyPressed(for: self)
    }

    fileprivate func deletePressed(event: NSEvent) -> Bool {
        guard let firstChar = event.characters?.first, let uniScalar = firstChar.unicodeScalars.first else {
            return false
        }

        let char = Int(uniScalar.value)

        switch char {
        case NSDeleteFunctionKey, NSDeleteCharFunctionKey, NSDeleteCharacter:
            return true
        default:
            return false
        }
    }
}

Here's a Swift 4 version of the answer that @paulmelnikow gave.

protocol MyTableViewDelegate: NSTableViewDelegate {
    func deleteKeyPressed(for tableView: MyTableView)
}

class MyTableView: NSTableView {
    override func keyDown(with event: NSEvent) {
        guard let myDelegate = delegate as? MyTableViewDelegate, deletePressed(event: event) else {
            super.keyDown(with: event)
            return
        }

        myDelegate.deleteKeyPressed(for: self)
    }

    fileprivate func deletePressed(event: NSEvent) -> Bool {
        guard let firstChar = event.characters?.first, let uniScalar = firstChar.unicodeScalars.first else {
            return false
        }

        let char = Int(uniScalar.value)

        switch char {
        case NSDeleteFunctionKey, NSDeleteCharFunctionKey, NSDeleteCharacter:
            return true
        default:
            return false
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文