0

I have a NSTableView with an NSTableColumn with an NSButtonCell (a checkbox) inside it, which generates a new instance of NSButtonCell each time a row is added, which I configured in IB. However, I'm curious why in the following chunk of code the second NSLog returns 0.

NSLog(@"%ld", (long)[[self.tableView preparedCellAtColumn:0 row:0]state]);
[[self.tableView preparedCellAtColumn:0 row:0]setState:1];
NSLog(@"%ld", (long)[[self.tableView preparedCellAtColumn:0 row:0]state]);

The fact that it returns 0 means that I am sending a message to an instance of NSButtonCell, right? So why doesn't setState: change the return value of the second NSLog?

PopKernel
  • 4,110
  • 5
  • 29
  • 51

1 Answers1

0

If it was receiving nil it would also print 0, I would suggest trying this

NSLog(@"cell:%@", [self.tableView preparedCellAtColumn:0 row:0])

to ensure you are actually getting a valid cell object from the table view.

Where are you calling that code from? After the table is already being displayed? During initialisation?

If the former then there should be a cell available, if the latter then it may not have been created or reallocated from the pool yet.

Try the NSLog command above to ensure you are actually getting a cell back and not nil from the table view.

Paige DePol
  • 1,121
  • 1
  • 9
  • 23
  • Yeah, it I tried calling it both in ApplicationDidFinishLaunching: and an IBAction AddTask: which fires when a button is clicked, and they both return along the lines of cell:. So, I know there's an instance there. However, calling setState: on it doesn't cause it's appearance to change. Actually, what would be most useful though is if I could initialize it with a certain state, as I'm trying to figure out a way to load the checkboxes state from a plist. At first I was thinking there must be a method like initWithState: but no such method exists… – PopKernel Jan 26 '14 at 17:09
  • As your checkbox is in a row in a table view your data source is how you should be setting the state of your checkbox, not programatically. When you want to change the state of the checkbox you need to update your data source, which would then update the checkbox in the table view. If you change it manually as you are trying above it would just be changed back to whatever value the data source returns for that row. Using the data source is also how you would set the states for all your checkboxes initially. – Paige DePol Jan 26 '14 at 19:43
  • For more information check out this page on Apple's developer website: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/TableView/PopulatingView-TablesProgrammatically/PopulatingView-TablesProgrammatically.html – Paige DePol Jan 26 '14 at 19:43
  • Thanks for the link. Believe it or not, I'm not sure that article was in the Xcode docsets, which explains why I missed it. I'll download it and look over it later. – PopKernel Jan 26 '14 at 22:58
  • I tend to use the documentation on the website more than the built-in docsets. Probably because back when I started in the 10.4 days it was just easier to google the class name and the first result was generally a link to the apple developer website. :) – Paige DePol Jan 26 '14 at 23:00