【问题标题】:How to implement a swipeable NSTableView cell?如何实现可滑动的 NSTableView 单元格?
【发布时间】:2018-07-22 08:03:24
【问题描述】:

NSTableViewRowAction 的简单示例? 向左/向右滚动以删除或执行其他操作。

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

【问题讨论】:

    标签: objective-c macos nstableview nstablerowview


    【解决方案1】:

    您只需要实现一个 NSTableViewDelegate method 来返回封装表格行操作的样式、标题和处理程序代码的操作:

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

    请注意,返回值是一个数组,因此每行可以返回多个操作。如果您想为每个滑动方向返回不同的操作,可以通过检查传递给委托方法的 edge 参数来实现。

    【讨论】:

    • 谢谢!一个疑问,长时间滑动(像iOS一样删除)这可能吗?怎么样?
    • 是的,如果您长滑超过某个阈值,该操作会自动执行。
    • 它似乎只适用于NSTableViewRowActionStyleDestructive 而不适用于NSTableViewRowActionStyleRegular 你能确认一下吗?
    • 没有。也适用于我的非破坏性行动。唯一的区别似乎是如果样式设置为破坏性,动作标签不会自动向后滑动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2011-11-20
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多