【问题标题】:Implementing NSTableViewRowAction using Swift使用 Swift 实现 NSTableViewRowAction
【发布时间】:2018-04-12 11:46:13
【问题描述】:

这应该很简单,但我不明白如何实现此操作。

我使用 Objective-c 找到了这个参考,但我想使用 swift 来做到这一点:

- (NSArray<NSTableViewRowAction *> *)tableView:(NSTableView *)tableView rowActionsForRow:(NSInteger)row edge:(NSTableRowActionEdge)edge {
    NSTableViewRowAction *action = [NSTableViewRowAction rowActionWithStyle:NSTableViewRowActionStyleDestructive title:@"Delete"
        handler:^(NSTableViewRowAction * _Nonnull action, NSInteger row) {
        // TODO: You code to delete from your model here.
        NSLog(@"Delete");
    }];
    return @[action];
}

我知道我需要实现该功能但不知道如何实现该方法。 我是 macOS 开发新手,在 App Store 上为 iOS 开发了两个应用程序,我认为将它们移植到 MacOS 会相对简单,我的错误!!

任何帮助表示赞赏。

【问题讨论】:

    标签: swift macos nstableview


    【解决方案1】:

    在 macOS 10.11 中添加了可滑动表格,因此要访问该功能,您需要在表格委托上实现此 NSTableViewDelegate 方法。例如,为您的视图控制器添加扩展就像这样简单:

    extension ViewController: NSTableViewDelegate {
    
        func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableRowActionEdge) -> [NSTableViewRowAction] {
            // left swipe
            if edge == .trailing {
                let deleteAction = NSTableViewRowAction(style: .destructive, title: "Delete", handler: { (rowAction, row) in
                    // action code
                })
    
                deleteAction.backgroundColor = NSColor.red
                return [deleteAction]
            }
    
            let archiveAction = NSTableViewRowAction(style: .regular, title: "Archive", handler: { (rowAction, row) in
                // action code
            })
    
            return [archiveAction]
        }
    }
    

    【讨论】:

    • 我每分钟都觉得自己越来越笨了,因为我看不出在哪里说声谢谢 mfessenden,而且这个输入字段似乎在说我不应该用它来表示感谢,但是,什么地狱,谢谢。我现在需要弄清楚为什么我自己搞不明白
    • 不客气。如果对您有用,请随意将问题标记为“已回答”:)
    猜你喜欢
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2015-03-24
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多