问题
This should be simple but I cannot understand how to implement this action.
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];
}
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!!
Any help appreciated.
回答1:
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]
}
}
来源:https://stackoverflow.com/questions/49795726/implementing-nstableviewrowaction-using-swift