1

A simple example for NSTableViewRowAction? Scroll left/right to delete or other action.

- (NSArray<NSTableViewRowAction *> *)tableView:(NSTableView *)tableView rowActionsForRow:(NSInteger)row edge:(NSTableRowActionEdge)edge
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
Joannes
  • 2,569
  • 3
  • 17
  • 39

1 Answers1

2

You just have to implement a NSTableViewDelegate method that returns action(s) that encapsulate the style, title and handler code for your table row action:

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

Note that the return value is an array so you can return more than one action per row. If you want to return different actions for each swipe direction, you can do that by checking the edge parameter that gets passed into the delegate method.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Thank you! A doubt, for a long swipe (to delete like iOS) is this possible? How? – Joannes Feb 12 '18 at 08:59
  • Yes, the action is automatically performed if you long-swipe over a certain threshold. – Thomas Zoechling Feb 12 '18 at 09:26
  • It seems available only for `NSTableViewRowActionStyleDestructive` and not for `NSTableViewRowActionStyleRegular` Can you confirm? – Joannes Feb 12 '18 at 10:20
  • 1
    No. Also works for non-destructive actions for me. Only difference seems to be that the action labels don't slide back automatically if style is set to destructive. – Thomas Zoechling Feb 12 '18 at 10:31