objective-cmacosnstableviewnstablerowview

How to implement a swipeable NSTableView cell?


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

- (NSArray<NSTableViewRowAction *> *)tableView:(NSTableView *)tableView rowActionsForRow:(NSInteger)row edge:(NSTableRowActionEdge)edge

Solution

  • 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.