NSTableView select row by pressing “space”

跟風遠走 提交于 2019-12-11 01:58:32

问题


I didn't find any information on this question, and I wonder if it is possible to make NSTableView (or subclass) to select rows by pressing space bar on keyboard, and navigate through rows by pressing up/down keys without the selection being reset. I want to make nstableview to behave like total commander's file panel, if someone used it under windows. And I don't even know where to start.


回答1:


You will have to make subclass of the NSTableView class. This is basic example how you could do it. It handles selection with the spacebar and with the right mouse button, hoever it does not handle right mouse button drag selection.

The idea is to use NSTableView in single select mode and implement alternative selection. We add property markedRows and then use it instead of original selectedRows property.

FOTableView.h

#import <Cocoa/Cocoa.h>

@interface FOTableView : NSTableView

@property (strong,nonatomic) NSMutableIndexSet *markedRows;

@end

FOTableView.m

#import "FOTableView.h"

@implementation FOTableView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }

    return self;
}

-(NSMutableIndexSet *) markedRows
{
    if (!_markedRows) {
        _markedRows = [NSMutableIndexSet new];
    }
    return _markedRows;
}

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
    if ([self.markedRows containsIndex:row]) {
        NSRect clipRect = [self rectOfRow:row];
        NSColor *color =  [NSColor colorWithCalibratedRed:0.932 green:0.046 blue:0.960 alpha:1.000];
        [color setFill];
        NSRectFill(clipRect);
    }

    [super drawRow:row clipRect:clipRect];
}

- (void)keyDown:(NSEvent *)theEvent
{
    NSString *keyString;
    unichar  keyChar;

    keyString = [theEvent charactersIgnoringModifiers];
    keyChar = [keyString characterAtIndex:0];
    NSInteger row = [self selectedRow];
    switch(keyChar){            
        case 32:
        {
             if (row != -1)
             {
                 if ([self.markedRows containsIndex:row]) {
                     [self.markedRows removeIndex:row];
                 }
                 else {
                     [self.markedRows addIndex:row];
                 }
             }
            [self selectRowIndexes:[NSIndexSet indexSetWithIndex:++row] byExtendingSelection:NO];
            [self setNeedsDisplay:YES];
            break;
        }

        default:
            [super keyDown:theEvent];
    }

    NSLog(@"key pressed: (%hu)%@", keyChar,keyString);
}

- (void)rightMouseDown:(NSEvent *)theEvent
{
    NSInteger row = [self rowAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
    if ([self.markedRows containsIndex:row]) {
        [self.markedRows removeIndex:row];
    }
    else {
        [self.markedRows addIndex:row];
    }

    [self setNeedsDisplay:YES];
}

@end


来源:https://stackoverflow.com/questions/12715826/nstableview-select-row-by-pressing-space

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