NSTableView with Checkbox cell

After adding an NSTableView to my xib in Xcode 4, I set 4 columns for it. The first column is a simple column that will contain the name of the element. The remaining 3 are flags. I dragged the Check Box Cell from the object library to the table.

I fill out the table and checkboxes are created and displayed, however, if I click on the fact that nothing is happening, I cannot check or uncheck them. Also, I donโ€™t even know how to do this with code.

How can I do this job: be able to check or uncheck the boxes and get their status from the code.

I already saw this question , and he did not answer my question.

Here are some of the codes that you must follow in the table:

 - (int)numberOfRowsInTableView:(NSTableView *)tableView { return (int)[myArray count]; } - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row { if([[tableColumn identifier] isEqualToString:@"col1"]) { return[NSNumber numberWithInt:NSOffState]; } return [myArray objectAtIndex:row]; } - (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { NSLog(@"%d", [anObject boolValue]); if([[tableColumn identifier] isEqualToString:@"col1"]) { NSLog(@"click col1"); } if([[tableColumn identifier] isEqualToString:@"col2"]) { NSLog(@"click col2"); } } 

I just added more code. How to set it to check / uncheck the box?

+6
source share
1 answer

Model

You need to choose a model, that is, how you are going to represent the data that appears in the table view. For instance:

 // SomeObject.h #import <Foundation/Foundation.h> @interface SomeObject @property (copy) NSString *name; @property (assign,getter=isVisible) BOOL visible; @property (assign,getter=isOpaque) BOOL opaque; @property (assign,getter=isAnimatable) BOOL animatable; @end // SomeObject.m #import "SomeObject.h" @implementation SomeObject @synthesize name, visible, opaque, animatable; - (void)dealloc { [name release]; [super dealloc]; } @end 

Nib file

For this answer, give the identifiers of the table columns that match the property names in SomeObject .

Providing values โ€‹โ€‹from a model as a table

 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row { // Retrieve the model object corresponding to `row' SomeObject *obj = [myArray objectAtIndex:row]; // Return the object property corresponding to the column if([[tableColumn identifier] isEqualToString:@"name"]) { return obj.name; } // Since this method has return type `id', we need to box the // boolean values inside an `NSNumber' instance else if([[tableColumn identifier] isEqualToString:@"visible"]) { return [NSNumber numberWithBool:obj.visible]; } else if([[tableColumn identifier] isEqualToString:@"opaque"]) { return [NSNumber numberWithBool:obj.opaque]; } else if([[tableColumn identifier] isEqualToString:@"animatable"]) { return [NSNumber numberWithBool:obj.animatable]; } return nil; } 

Using values โ€‹โ€‹from a table view to update a model

 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { // Retrieve the model object corresponding to `row' SomeObject *obj = [myArray objectAtIndex:row]; // Set the object property corresponding to the column if([[tableColumn identifier] isEqualToString:@"name"]) { obj.name = anObject; } // Since the new value (`anObject') is an object, we need to // convert it to `BOOL' by sending it `-boolValue' else if([[tableColumn identifier] isEqualToString:@"visible"]) { obj.visible = [anObject boolValue]; } else if([[tableColumn identifier] isEqualToString:@"opaque"]) { obj.opaque = [anObject boolValue]; } else if([[tableColumn identifier] isEqualToString:@"animatable"]) { obj.animatable = [anObject boolValue]; } } 

You can make this code simpler by using keyword coding, but this is left as an exercise after you study the data sources of the table .: P

+8
source

Source: https://habr.com/ru/post/897395/


All Articles