使用Swift实现NSTableViewRowAction [英] Implementing NSTableViewRowAction using Swift
问题描述
这应该很简单,但是我不明白如何执行此操作.
This should be simple but I cannot understand how to implement this action.
我使用Objective-C找到了这个参考,但是我想使用swift:
I found this reference using objective-c but I want to do this using 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相对简单,我的错!
I understand I need to implement the function but do not know how to implement the method. I am new to macOS development having developed two apps for iOS on the App Store I figured porting them to MacOS would be relatively simple, my mistake!!
任何帮助表示赞赏.
推荐答案
在macOS 10.11中添加了可滑动表,因此要访问功能,您需要在表委托上实现此NSTableViewDelegate
方法.例如,为视图控制器添加扩展将很简单:
Swipeable tables were added in macOS 10.11, so to access the functionality you'll need to implement this NSTableViewDelegate
method on your table delegate. For example, adding an extension for your view controller would be as simple as:
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实现NSTableViewRowAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!