NSTableView – Handling Tab and Return Keys

NSTableView – Handling Tab and Return Keys

In this article, we have implemented the following behavior in a NSTableView.

1. Handling Tab text movements in NSTableView to navigate from the last column of the table to the first column of the next row.

2. Handling Return Text movement in NSTableView to navigate from the last row of the table to the first row of the next column.

Refer to the below screenshots for the navigation behavior.

Handling Tab Text Movement in NSTableView

Tab_NSTableView

 

Handling Return Text Movement in NSTableView

Return_NSTableView

//
// DDTableView.h
// TableViewDemo
//
// Created by Debasis Das on 3/17/14.
// Copyright (c) 2014 Debasis Das. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface DDTableView : NSTableView
@end
//
// DDTableView.m
// TableViewDemo
//
// Created by Debasis Das on 3/17/14.
// Copyright (c) 2014 Debasis Das. All rights reserved.
//
//
#import "DDTableView.h"
@implementation DDTableView
//Subclass NSTableView and override the textDidEndEditing method. Then change the custom class of NSTableView instance in IB to the subclass. 
- (void) textDidEndEditing: (NSNotification *) notification
{
     NSInteger editedColumn = [self editedColumn];
     NSInteger editedRow = [self editedRow];
     NSInteger lastRow = [self numberOfRows];
     NSInteger lastCol = [self numberOfColumns];
     NSDictionary *userInfo = [notification userInfo];
     int textMovement = [(NSNumber *)[userInfo valueForKey:@"NSTextMovement"] intValue];
     [super textDidEndEditing: notification];

    if (textMovement == NSTabTextMovement)
    {
        if (editedColumn != lastCol - 1 )
        {
          [self selectRowIndexes:[NSIndexSet indexSetWithIndex:editedRow] byExtendingSelection:NO];
          [self editColumn: editedColumn+1 row: editedRow withEvent: nil select: YES];
        }
       else
        {
          if (editedRow !=lastRow-1)
          {
            [self editColumn:0 row:editedRow + 1 withEvent:nil select:YES];
          }
          else
          {
           [self editColumn:0 row:0 withEvent:nil select:YES]; // Go to the first cell
          }
       }
    }
   else if (textMovement == NSReturnTextMovement)
   {
       if(editedRow !=lastRow-1)
       {
          [self selectRowIndexes:[NSIndexSet indexSetWithIndex:editedRow+1] byExtendingSelection:NO];
          [self editColumn: editedColumn row: editedRow+1 withEvent: nil select: YES];
       }
       else
       {
         if (editedColumn !=lastCol - 1)
         {
           [self editColumn:editedColumn+1 row:0 withEvent:nil select:YES];
         }
         else
         {
           [self editColumn:0 row:0 withEvent:nil select:YES]; //Go to the first cell
         }
       }
    }
}
@end
Posted in Cocoa, Objective C Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news