0

How can one have checkboxes and textfield (for section headings) in a single tableview column using NSTableViewDataSource Protocol?

My requirement is to use a Cell Based TableView.

bhartsb
  • 1,316
  • 14
  • 39

2 Answers2

0

I answered your other question without any code and i think you had trouble understanding it.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    array = [[NSMutableArray alloc]initWithObjects:@0,@1,@2, nil];//instead this you can add your class object
    [self.myTableView reloadData];

}


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [array count];
}


- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSButtonCell * cell =[[NSButtonCell alloc]init];
    [cell setButtonType:NSSwitchButton];

    if([array objectAtIndex:row] == [NSNumber numberWithInt:0])
    {
        [tableColumn setDataCell:cell];
        [[tableColumn dataCell]setTitle:@"Are you single?"];// instead this you can access title from your class object or from any other storage
    }
    else if ([array objectAtIndex:row] == [NSNumber numberWithInt:1])
    {
        [tableColumn setDataCell:[[NSTextFieldCell alloc]init]];

    }
    else if ([array objectAtIndex:row] == [NSNumber numberWithInt:2])
    {
        [tableColumn setDataCell:cell];
        [[tableColumn dataCell]setTitle:@"Are you happy?"];
    }

    return [array objectAtIndex:row];
}

So thought this would help:) Cheers.

Suhas Aithal
  • 842
  • 8
  • 20
  • Above code works fine ,You can also do similar stuff in - (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row; – Suhas Aithal Jul 04 '13 at 09:00
  • I tried your code as posted. The NSTextFieldCell doesn't display a title (only a 1) and the button states are fixed (won't change when clicked). If I add - (void)tableView:(NSTableView *)tableView setObjectValue:(id)value forTableColumn:(NSTableColumn *)column row:(NSInteger)row I get strange behavior. Can you please update your code example with whatever is needed so that the NSTextFieldCell shows a title as the checkboxes do, and so that the checkbox buttons can be toggled? Thanks. – bhartsb Jul 05 '13 at 07:40
  • You can not expect us to code for you, I am sure you can take that code as an example and get what you want:) Text field shows one coz i have added @0,@1,@2 these as content, look at my comment in that code you can add anything you want. Use seObjectValue method to change the state of check box. . comment if you fail. i will edit answer as required. – Suhas Aithal Jul 05 '13 at 09:11
  • Using an array to filter on the row index was distracting and I'm not sure the point of it in the context of the questions I've been posting, and which you've commented on at least two. I'm referring to the lines: if ([array objectAtIndex:row] == [NSNumber numberWithInt:0]){…} else if([array objectAtIndex:row] == [NSNumber numberWithInt:1]) {…} else if([array objectAtIndex:row] == [NSNumber numberWithInt:2]) {…} – bhartsb Jul 05 '13 at 18:56
  • Dude i just showed you can have textfields and checkboxes in your single column and even you can have different titles too:). . most importantly answered your question which asked"HOW" . I am least bothered about implimentation details coz it is your job – Suhas Aithal Jul 08 '13 at 05:25
  • Yes and recall that you replied to another post where I related I had figured out (on my own) how to do the same with dataCellForTableColumn:. The issue I expressed that I was still having difficulty with was that of setting the text for NSTextFieldCell. However rather than just being helpful by agreeably clarifying, you become touchy as demonstrated by your comment "You can not expect us to code for you". I wasn't asking anyone to "code for me" but rather for a clear example of how achieve something specific with the TableView control. – bhartsb Jul 08 '13 at 17:34
  • I appreciated your trying to help, but not the attitude. Your posts were hard to understand, because the code given was incomplete (with many details omitted) and the writing poor. As for posting an answer that fills in the holes of your answer, I think that others likely will appreciate that I took the time to do it. It wasn't about taking credit, but rather about making this post more useful to others. – bhartsb Jul 08 '13 at 17:35
  • Note: I would have posted my answer as a comment to Suhas's answer but comments have size limits and don't allow the same formatting options. – bhartsb Jul 08 '13 at 17:42
  • Attitude? common i just wanted you to try and learn more ,so that you get that knowledge which no one else can teach you by spoon feeding. i ts okie chill. i am glad that you finaly have your answer and thats what matters the most. – Suhas Aithal Jul 09 '13 at 04:36
0

Here are the steps to make a single column tableview where the column can have row(s) that are section headings (NSTextFieldCells) followed by rows that are checkboxes (NSButtonCells) having descriptive titles. Similar to a listbox in MS MFC. To be compatible with older versions of OS X it needs to be a Cell based tableview:

  1. Using IB drag a tableView control into the Application Window. In the Attributes inspector set Content Mode as "Cell Based", and Columns to 1.
  2. Using IB drag a "Check Box Cell" control from the Object Library into the Application Window's column. (note: this step probably can be omitted since in the example code shown below, the cell type is being set explicitly to be either a NSButtonCell (checkbox) or NSTextFieldCell). If one needs to expand this example to use multiple columns, then probably want to set the Identifier for the NSTableColumn(s) in IB's Identity Inspector, in order that in the code one can filter by column/row instead of only by row (i.e. inside of the method objectValueForTableColumn).
  3. Set the TableView's datasource and delegate to be the auto generated App Delegate object (in this case ApplicationAppDelegate.h). Do this by opening IB, and using the "Connection Inspector" click and drag from the datasource circle connection icon to the "App Delegate" object icon in the IB panel that shows objects that are loaded from the NIB such as controls, controllers etc.(icon for App Delegate is a blue cube). Do the same click and drag operation with the delegate circle icon.
  4. Open the Assistant Editor, with the App Delegate's .h file showing in the left vertical pane and the IB view of the Table View in the right vertical pane. Select on the TableView control and create an IB outlet named "tableView" by holding the control key and dragging from the TableView Control to the section of the .h file where properties are listed.
  5. Declare a NSMutableArray variable in the .h file. It should look like the following (Note: there has been added NSTableViewDataSource as a supported protocol of ApplicationAppDelegate):

#import <Cocoa/Cocoa.h>

@interface ApplicationAppDelegate : NSObject <NSApplicationDelegate,NSTableViewDataSource>
{
   NSMutableArray *state;
}
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTableView *tableView;

@end

6 . Add the following functions to the App Delegate implementation file (.m):


#import "ApplicationAppDelegate.h"
@implementation ApplicationAppDelegate  

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    state = [[NSMutableArray alloc]initWithObjects:@"Section Heading:",@0,@1, nil];//Note: values passed to NSButtonCells should be 0 or 1 or YES or NO, and the state passed to NSTextFieldCell is a NSString
    [self.tableView reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [state count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSButtonCell * cell =[[NSButtonCell alloc]init];
    [cell setButtonType:NSSwitchButton];

    if (row == 0)
    {
        [tableColumn setDataCell:[[NSTextFieldCell alloc]init]];
    }
    else if (row == 1)
    {
        [tableColumn setDataCell:cell];
        [[tableColumn dataCell]setTitle:@"title row1"];
    }
    else if (row == 2)
    {
        [tableColumn setDataCell:cell];
        [[tableColumn dataCell]setTitle:@"title row2"];
    }
    return [state objectAtIndex:row];

}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)value forTableColumn:(NSTableColumn *)column row:(NSInteger)row 
{
   [state replaceObjectAtIndex:row withObject:value];
   [tableView reloadData];
}

@end
bhartsb
  • 1,316
  • 14
  • 39