bhartsb
bhartsb

Reputation: 1356

How to set the attributes of checkboxes in a single column tableview

I'm reviewing a sample program where a series of checkboxes were presumably created in a tableview by dragging the Checkbox cell into a single column using IB. In the sample the AppDelegate was the datasource with implementations of numberOfRowsInTableView:, setObjectValue:, and objectValueForTableColumn:.

In objectValueForTableColumn: the state of the checkboxes is being set with '0' or '1' in an array being returned.

What I'm unclear about is:

1) How to set the attributes of the checkbox in each row? For example how does one set the checkbox title? In the sample each checkbox of each row is titled "checkbox". I want each to have a unique title for each checkbox in each row.

2) How to set other checkbox button attributes such as enabled/disabled etc. ?

3) How are the checkboxes getting created? I'm assuming that they are somehow created after numberOfRowsInTableView: is called, when objects in the NIB are being instantiated. How does one best access these objects to do things like changing of attributes? Apple's TableView document for all its wordiness seems to lack any examples that would clarify.

Upvotes: 0

Views: 2270

Answers (2)

Abdul Naveed
Abdul Naveed

Reputation: 587

As per the comment "I appreciate the code example, but unfortunately I need a cell based tableview in order to run on versions of OS X prior to 10.7."

In that case, You can comment the method

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

And you can use the delegate method(willDisplayCell) for that replacement. Make sure you remove the method and make the table view as cell based. Drag and drop check box(NSButtonCell). Implement the same. Rest all remains the same.

/* Delegate Method For the same example explained in the above answer */
    - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
    {
        NSUInteger tableCol = [tableView.tableColumns indexOfObject:tableColumn];
        if(tableCol == 0)
        {
            NSDictionary *valueDictionary = [self.myArray objectAtIndex:row];
            NSString *titleValue = [valueDictionary valueForKey:@"Name"];
            [cell setTitle:titleValue];

            NSUInteger mod = row % 2;
            if(mod == 0)
            {
                [cell setState:NSOnState];
            }
            else
            {
                [cell setState:NSOffState];
            }
        }
        else
        {
            [cell setTitle:@"Column 2"];        
        }
    }

This should be what you are expecting : )

For your information: The Table View Object should be in following way:

enter image description here

And the Table View Bindings should be in the below way

enter image description here

And the Array Controller Binding should be in the below way:

enter image description here

Let me if you are still facing the issue.

Upvotes: 2

Abdul Naveed
Abdul Naveed

Reputation: 587

This is just an example.

Follow the Steps to accomplish:

  1. Drag and Drop a table View.
  2. Drag and Drop a table Cell View (NSTableCellView) inside the Table View Cell. Now, Your table view is view based.
  3. Set The Identifier for table Cell View (NSTableCellView) as "CheckBoxCell" -> NSTableCellView of the first Column and the other column's table cell view as "TextBoxCell".
  4. Delete the image view present inside the NSTableCellView.
  5. Drag and Drop a Check Box(Not NSButtonCell, it should be NSButton).
  6. Drag and Drop array controller and place an outlet "myArrayController"
@interface ExAppDelegate : NSObject <NSApplicationDelegate,NSTableViewDelegate>

@property (assign) IBOutlet NSWindow *window;


@property (strong) NSMutableArray *myArray;

@property (strong) IBOutlet NSTableView *myTableView;

@property (strong) IBOutlet NSArrayController *myArrayController;

@end
@implementation ExAppDelegate
@synthesize myArray;
@synthesize myTableView;

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
    [self.myTableView setDelegate:self];
    if(self.myArray == nil)
    {
        self.myArray = [NSMutableArray array];
        for (NSUInteger i=0; i < 10; i++)
        {
            NSMutableDictionary *test = [NSMutableDictionary dictionary];
            [test setValue:[NSString stringWithFormat:@"%ld",i] forKey:@"Name"];
            NSUInteger marks = i + 10;
            [test setValue:[NSString stringWithFormat:@"%ld",marks] forKey:@"Marks"];
            [self.myArray addObject:test];
        }
    }
   [self.myArrayController rearrangeObjects];
}



- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *checkBoxCell = nil;

    if(tableView == self.myTableView)
    {
        //Get the Column Number
        NSUInteger tableCol = [tableView.tableColumns indexOfObject:tableColumn];
        if(tableCol == 0)
        {           
           checkBoxCell = (NSTableCellView *)[tableView makeViewWithIdentifier:@"CheckBoxCell" owner:tableColumn];
            NSArray *myViews = [checkBoxCell subviews];
            NSButton *checkBoxButton = [myViews objectAtIndex:0];

            NSDictionary *valueDictionary = [self.myArray objectAtIndex:row];
            NSString *titleValue = [valueDictionary valueForKey:@"Name"];
            [checkBoxButton setTitle:titleValue];

            NSUInteger mod = row % 2;
            if(mod == 0)
            {
                [checkBoxButton setState:NSOnState];
            }
            else
            {
                [checkBoxButton setState:NSOffState];
            }
        }
        else
        {
            checkBoxCell = (NSTableCellView *)[tableView makeViewWithIdentifier:@"TextBoxCell" owner:tableColumn];

        }
    }   

    return checkBoxCell;
}

@end

=============================================

Bindings(Ignoring Data Source) myArrayController - > bind to - > ExAppDelegate - > Content Array - > model key Path -> self.myArray tableView - > bind to -> Table Content - > Content Array - > arrayController -> arranged Objects. DO NOT SET ANY MODEL KEY PATH FOR TABLE VIEW BINDINGS

=============================================

  1. I have set a Outlet as mytableView to NSTableView
  2. Assuming Two Columns in a Table View
  3. Assuming one column should be set with a Check Box Cell and the other with a Text Field Cell

*You want to set the Unique title to the check box cell along with marking as checked or not. (FYI : I alternatively marking checked and unchecked, Like 1st row will be checked and the second will be unchecked and so on...)

  1. I'm considering an Array Containing a Dictionary with 2 Keys -- > 1st Key (Name) 2nd Key(Marks)

  2. Then Later Implement the method "- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row"

    I Hope this will be help in fixing the issue. :)

Upvotes: 2

Related Questions