We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

Netzach on 01/17/08


Tagged

textmate cocoa method ruby rubycocoa NSTableView


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

Netzach


RubyCocoa control methods for a four column NSTableView


Published in: Ruby 


  1. # What you NEED in the controller to be able to list someting in a NSTableView in RubyCocoa
  2. #
  3. # Creates the storage array, and another array which contains the names of the columns.
  4. def initialize
  5. @objects = Array.new
  6. @columns = ["Column Name 1", "Column Name 2", "Column Name 3", "Column Name 4"]
  7. end
  8.  
  9. # Returns the numbers of rows in the array for NSTableView to display.
  10. def numberOfRowsInTableView(aTableView)
  11. return @objects.length
  12. end
  13.  
  14. # Uses the @columns array to to enable a flexible number of columns when fetching data.
  15. def tableView_objectValueForTableColumn_row(afileTable, aTableColumn, rowIndex)
  16. @columns.each do |column|
  17. if aTableColumn.headerCell.stringValue == column
  18. object = @objects[rowIndex]
  19. return object[column]
  20. end
  21. end
  22. end
  23.  
  24. # Usage:
  25. #
  26. # Creates the object that will go into the array. Mind you, the key values must be named the same way as the columns in the NSTableView, which is why it might be a good idea to use the actual @columns array post a identifiers.
  27. object = {
  28. @columns[0] => "Put...",
  29. @columns[1] => "...your...",
  30. @columns[2] => "...values...",
  31. @columns[3] => "...here"
  32. }
  33.  
  34. # Adds the object to the @objects array
  35. @objects += [object]
  36.  
  37. # Updates the NSTableView after a new post/row has been added to the storage array
  38. @tableViewOutlet.reloadData

Report this snippet 

You need to login to post a comment.