Implementing NSTableViewRowAction using Swift

风格不统一 提交于 2019-12-11 17:58:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!