0

A TableView is created in IB, and it is desired that it have only one column with checkboxes. One way to create the checkboxes is to drag an NSButtonCell into the column using IB, and then conforming to the NSTableViewDataSource protocol implement:

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView

Hence when a class object is acting as a datasource to a column in a TableView, and the column contains objects such as checkboxes for each row, is the class supplying the only the on/off state data for the checkboxes, or is it also supplying the checkbox objects?

Related questions are:

  1. Is the TableView creating and containing the NSButtonCells?
  2. If the answer to 1 is yes, how can one set/change the NSButtonCell's properties, like its title?
  3. Rather than the TableView creating and maintaining the NSButtonCells, can the datasource class create them? That is, instead of using IB to drag the NSButtonCell to the column, can one add the NSButtonCells in a method such as awakeFromNIB?
  4. Is there a way to mix the types of cell objects in a given column. For example, could one have a text heading in column1row1 followed by a checkbox in column1row2?

Thanks.

bhartsb
  • 1,316
  • 14
  • 39

1 Answers1

0

I think you are finding it difficult to understand the difference between Model,View and the Controller.

Your Class object should only act as "Model" - in your case supply the on/off state data. Your IB provides the "View" part ,Normally you should use it for creating any user interface. Your View Controller class which impliments tableview delegate/datsource methods is the"Controller" part and you should use it as a mediator between View and Model.

You can choose to provide button state along with title in your Model. and can set it in your Controllers implementation of the delegate/datasource method.

Yes, you can create button cells in your controller but avoid it unless it is absolutely necessary.

You can mix different types of cells,Instead you can choose to look into the View based TableView also.

Suhas Aithal
  • 842
  • 8
  • 20
  • I understand Model, View, Controllers perfectly well. I think that the reason I haven't had received detailed answers to my question (from you or anyone) is that perhaps the details on TableView are no longer widely known. I had to look at many Cocoa books before I found one that I think gives the detailed information: Cocoa® Programming By Scott Anguish, Erik M. Buck, Donald A. Yacktman (publish date 2002)has a section on TableView that gives details that none of the newer Cocoa books I reviewed seem to have. – bhartsb Jul 03 '13 at 21:24
  • I already know how to set the state of a checkbox. What I didn't know was question # 2: how can one set/change the NSButtonCell's properties, like its title? And by set title, I mean each cell's checkbox (in a single column table) has its own title, not all of them with the same title. Simlarly, how to have rows in a single column in some cells contain a checkbox and other cells contain a textfieldCell (like for section headings for grouping rows of checkboxes). – bhartsb Jul 03 '13 at 21:43
  • The book states that by default, the entries in the table are NSTextFieldCell objects, with one cell object per column which is shared by all rows of that column. So in order to set the title of the checkbox one would, a) subclass NSTableColumn, b) Override the method - (id)dataCellForRow:(int)row , and in the body of this overridden method get the column's shared cell object (in this case a NSButtonCell checkbox dragged to the colummn in IB) from the superclass and set a unique title. – bhartsb Jul 03 '13 at 22:34
  • I'm further assuming (because I haven't yet tried this) that to insert a different cell type (different from the NSButtonCell checkbox setup in IB), one has to allocate&init the desired cell type and return it in dataCellForRow: – bhartsb Jul 03 '13 at 22:34
  • If one has to only set the properties (e.g. title) of the TableView column's shared cell object, then as an alternative to sub-classing, one might possibly add this method in the delegate: - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row. And note, if the cell object is a textFieldCell and not a NSButtonCell it wouldn't have a title property, so I'm talking here about the case of setting the title when it is a NSButtonCell. – bhartsb Jul 03 '13 at 22:40
  • I think you spent more time in proving youreself instead of understanding my answer:).I clearly answered your question #2. we are here to help. read it again and understand:) – Suhas Aithal Jul 04 '13 at 05:37