Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Created May 27, 2011 17:55
Show Gist options
  • Save danielwestendorf/995779 to your computer and use it in GitHub Desktop.
Save danielwestendorf/995779 to your computer and use it in GitHub Desktop.
Sub-classed NSTableView for CMD+A/Select all functionality
class SelectAllTable < NSTableView
attr_accessor :delegator #Connect this to the delegate of your table view
def keyDown(event) #any time this table is in focus and a key is pressed, this method will be called.
characters = event.characters
character = characters.characterAtIndex(0)#let's get the first key that was pressed
if character == 97 && (event.modifierFlags & NSCommandKeyMask) == NSCommandKeyMask #97 is the "A" key, while the CMD key is a modifier key. Check to see if both were pressed
@delegator.select_all #call your method that selects all your rows
else
super(event) #This isn't what we're looking for, pass it on to super
end
end
end
#example select_all method
def select_all
range = NSMakeRange(0, my_table_array.length) #set the range to include all rows
indexes = NSIndexSet.alloc.initWithIndexesInRange(range)
my_table_view.selectRowIndexes(indexes, byExtendingSelection:true) #visually show the indexes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment