How to one click on an NSTextFieldCell in an NSTableView to edit it?

The default is double-click. How can it be redefined?

+3
source share
5 answers

One way to implement one-click behavior is to drag and drop the method setAction. Here is an example of this for NSTableView, which allows the user to edit a single column from any row in the table by clicking on it once:

SingleClickAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface SingleClickAppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSTableView *theTable;
    IBOutlet NSArrayController *theArrayController;

    NSInteger colNumber;
}

- (void)addRow;
- (void)doClickEdit: (id)sender;

@end

SingleClickAppDelegate.m

#import "SingleClickAppDelegate.h"

@implementation SingleClickAppDelegate

- (void)awakeFromNib
{
    [self addRow];
    [self addRow];
    [self addRow];

    [theTable setTarget:self];

    /// Single click action
    [theTable setAction:@selector(doClickEdit:)];
}

/// Add some temporary data to edit
- (void)addRow
{
    NSMutableDictionary *values = [[NSMutableDictionary alloc] init];

    [values setObject:[NSNumber numberWithLong:colNumber++] forKey:@"col1"];
    [values setObject:@"Text" forKey:@"col2"];

    [theArrayController addObject:values];

    [values release];

    [theTable reloadData];
}

/// Action
- (void)doClickEdit: (id)sender
{
    NSLog(@"doClickEdit:");
    NSInteger row = [theTable clickedRow];
    [theTable editColumn:1 row:row withEvent:nil select:YES];
}

@end

Now create the bindings in the interface builder:

  • Create a class object SingleClickAppDelegatethat will interact with the above GUI code.
  • Create an NSTableView and bind it to theTable.
  • NSTableView dataSource SingleClickAppDelegate.
  • NSArrayController theArrayController.
  • :
    • theArrayController
    • .
    • ( "col1" "col2" )

, . .

, doClickEdit:.

+3

TableView,

- (void)editColumn:(NSInteger)columnIndex
               row:(NSInteger)rowIndex
         withEvent:(NSEvent *)theEven
            select:(BOOL)flag
+2

, - . .

0

, NSTableView :

myNSTableView.h

#import <Cocoa/Cocoa.h>
@interface myNSTableView : NSTableView {

}
- (void)singleClickEdit: (id)sender;
@end

myNSTableView.m

#import "myNSTableView.h"
@implementation myNSTableView

- (void)awakeFromNib
{
   [self setAction:@selector(singleClickEdit:)];
}

- (void)singleClickEdit: (id)sender
{
   NSLog(@"singleClickEdit:");
   [self editColumn:[self clickedColumn] row:[self clickedRow] withEvent:nil select:NO];
}

@end

NSTableView myNSTableview, :

#import <Cocoa/Cocoa.h>
#import "myNSTableView.h"

@interface Preference : NSPanel {
   NSMutableArray             *chargeArray; // Array of ChargeDefault objects
   IBOutlet    myNSTableView  *chargeTableView;
}

IB NSTableView, NSView. "Indentity Inspector" myNSTableView NSTableView.

.

0

Instead of subclassing NSTableView, you can simply create a category. In the header file of your view controller ( MyViewController.h or any file with IBOutlet associated with the table view), declare a new category:

@interface NSTableView( OneClickEdit )
- (void)singleClickEdit: (id)sender;
@end

@interface MyViewController : NSViewController
{
     IBOutlet NSTableView *tableView;
...

Then, in the corresponding .m file ( MyViewController.m ) add a schema implementation outside of the MyViewController implementation itself and make this new method the method of action of your table view in viewDidLoad your view controller:

@implementation NSTableView( OneClickEdit )
- (void)singleClickEdit: (id)sender
{
    [self editColumn:[self clickedColumn] row:[self clickedRow] withEvent:nil select:YES];
}
@end
@implementation MyViewContoller

- (void)viewDidLoad
{
    [super viewDidLoad];
    [tableView setAction:@selector(singleClickEdit:)];
....
0
source

All Articles