gpt4 book ai didi

swift - 使用 Swift 实现 NSTableViewRowAction

转载 作者:行者123 更新时间:2023-11-28 10:09:34 25 4
gpt4 key购买 nike

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

我使用 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 会相对简单,我的错误!!

感谢任何帮助。

最佳答案

在 macOS 10.11 中添加了可滑动表格,因此要访问您需要在表格委托(delegate)上实现此 NSTableViewDelegate 方法的功能。例如,为您的 View Controller 添加一个扩展就像这样简单:

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]
}
}

关于swift - 使用 Swift 实现 NSTableViewRowAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49795726/

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