1

I should start off by saying I am very very new to xcode. I find it intriguing and understand if you are annoyed by my unintelligence in this field. However if you feel you can help me I would be very grateful. Now, I have created an NSTable with 6 columns and successfully populated its cells with text. Now I want to take the last 2 cells and make them Check box Cells. I have dragged the button onto the .xib but the check marks do not click or unclick. So my questions is... How do I make these check marks work properly?

This is my .h code

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource> {

IBOutlet NSTableView *table;
NSMutableArray *BaseSet;

}

@property (assign) IBOutlet NSWindow *window;

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator         *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

- (IBAction)saveAction:(id)sender;

@end

This is my .m code.

#import "AppDelegate.h"

@implementation AppDelegate


- (void) awakeFromNib {

BaseSet = [[NSMutableArray alloc] init];

[BaseSet addObject:@{ @"No." : @"1", @"Card Name" : @"Alakazam", @"Type" : @"Psychic", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"2", @"Card Name" : @"Blastoise", @"Type" : @"Water", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"3", @"Card Name" : @"Chansey", @"Type" : @"Colorless", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"4", @"Card Name" : @"Charizard", @"Type" : @"Fire", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"5", @"Card Name" : @"Clefairy", @"Type" : @"Colorless", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"6", @"Card Name" : @"Gyarados", @"Type" : @"Water", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"7", @"Card Name" : @"Hitmonchan", @"Type" : @"Fighting", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"8", @"Card Name" : @"Machamp", @"Type" : @"Fighting", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"9", @"Card Name" : @"Magneton", @"Type" : @"Lightning", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"10", @"Card Name" : @"Mewtwo", @"Type" : @"Psychic", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"11", @"Card Name" : @"Nidoking", @"Type" : @"Grass", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"12", @"Card Name" : @"Nintales", @"Type" : @"Fire", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"13", @"Card Name" : @"Poliwrath", @"Type" : @"Water", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"14", @"Card Name" : @"Raichu", @"Type" : @"Lightning", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"15", @"Card Name" : @"Venusaur", @"Type" : @"Grass", @"Rarity" : @"★H" }];
[BaseSet addObject:@{ @"No." : @"16", @"Card Name" : @"Zapdos", @"Type" : @"Lightning", @"Rarity" : @"★H" }];

[BaseSet addObject:@{ @"No." : @"17", @"Card Name" : @"Beedrill", @"Type" : @"Grass", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"18", @"Card Name" : @"Dragonair", @"Type" : @"Colorless", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"19", @"Card Name" : @"Dugtrio", @"Type" : @"Fighting", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"20", @"Card Name" : @"Electabuzz", @"Type" : @"Lightning", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"21", @"Card Name" : @"Electrode", @"Type" : @"Lightning", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"22", @"Card Name" : @"Pidgeotto", @"Type" : @"Colorless", @"Rarity" : @"★" }];

[BaseSet addObject:@{ @"No." : @"23", @"Card Name" : @"Arcanine", @"Type" : @"Fire", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"24", @"Card Name" : @"Charmeleon", @"Type" : @"Fire", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"25", @"Card Name" : @"Dewgong", @"Type" : @"Water", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"26", @"Card Name" : @"Dratini", @"Type" : @"Colorless", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"27", @"Card Name" : @"Farfetch'd", @"Type" : @"Colorless", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"28", @"Card Name" : @"Growlithe", @"Type" : @"Fire", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"29", @"Card Name" : @"Haunter", @"Type" : @"Psychic", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"30", @"Card Name" : @"Ivysaur", @"Type" : @"Grass", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"31", @"Card Name" : @"Jynx", @"Type" : @"Psychic", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"32", @"Card Name" : @"Kadabra", @"Type" : @"Psychic", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"33", @"Card Name" : @"Kakuna", @"Type" : @"Grass", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"34", @"Card Name" : @"Machoke", @"Type" : @"Fighting", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"35", @"Card Name" : @"Magikarp", @"Type" : @"Water", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"36", @"Card Name" : @"Magmar", @"Type" : @"Fire", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"37", @"Card Name" : @"Nidorino", @"Type" : @"Grass", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"38", @"Card Name" : @"Poliwhirl", @"Type" : @"Water", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"39", @"Card Name" : @"Porygon", @"Type" : @"Colorless", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"40", @"Card Name" : @"Raticate", @"Type" : @"Colorless", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"41", @"Card Name" : @"Seel", @"Type" : @"Water", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"42", @"Card Name" : @"Wartortle", @"Type" : @"Water", @"Rarity" : @"♦" }];

[BaseSet addObject:@{ @"No." : @"43", @"Card Name" : @"Abra", @"Type" : @"Psychic", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"44", @"Card Name" : @"Bulbasaur", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"45", @"Card Name" : @"Caterpie", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"46", @"Card Name" : @"Charmander", @"Type" : @"Fire", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"47", @"Card Name" : @"Diglett", @"Type" : @"Fighting", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"48", @"Card Name" : @"Doduo", @"Type" : @"Colorless", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"49", @"Card Name" : @"Drowzee", @"Type" : @"Psychic", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"50", @"Card Name" : @"Gastly", @"Type" : @"Psychic", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"51", @"Card Name" : @"Koffing", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"52", @"Card Name" : @"Machop", @"Type" : @"Fighting", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"53", @"Card Name" : @"Magnemite", @"Type" : @"Lightning", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"54", @"Card Name" : @"Metapod", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"55", @"Card Name" : @"Nidoran♂", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"56", @"Card Name" : @"Onix", @"Type" : @"Fighting", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"57", @"Card Name" : @"Pidgey", @"Type" : @"Colorless", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"58", @"Card Name" : @"Pikachu", @"Type" : @"Lightning", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"59", @"Card Name" : @"Poliwag", @"Type" : @"Water", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"60", @"Card Name" : @"Ponyta", @"Type" : @"Fire", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"61", @"Card Name" : @"Rattata", @"Type" : @"Colorless", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"62", @"Card Name" : @"Squirtle", @"Type" : @"Water", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"64", @"Card Name" : @"Starmie", @"Type" : @"Water", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"65", @"Card Name" : @"Staryu", @"Type" : @"Water", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"66", @"Card Name" : @"Tangela", @"Type" : @"Grass", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"67", @"Card Name" : @"Voltorb", @"Type" : @"Lightning", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"68", @"Card Name" : @"Vulpix", @"Type" : @"Fire", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"69", @"Card Name" : @"Weedle", @"Type" : @"Grass", @"Rarity" : @"●" }];

[BaseSet addObject:@{ @"No." : @"70", @"Card Name" : @"Clefairy Doll", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"71", @"Card Name" : @"Computer Search", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"72", @"Card Name" : @"Devolution Spray", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"73", @"Card Name" : @"Imposter Professor Oak", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"74", @"Card Name" : @"Item Finder", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"75", @"Card Name" : @"Lass", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"76", @"Card Name" : @"Pokémon Breeder", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"77", @"Card Name" : @"Pokémon Trader", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"78", @"Card Name" : @"Scoop Up", @"Type" : @"Trainer", @"Rarity" : @"★" }];
[BaseSet addObject:@{ @"No." : @"79", @"Card Name" : @"Super Energy Removal", @"Type" : @"Trainer", @"Rarity" : @"★" }];

[BaseSet addObject:@{ @"No." : @"80", @"Card Name" : @"Defender", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"81", @"Card Name" : @"Energy Retrieval", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"82", @"Card Name" : @"Full Heal", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"83", @"Card Name" : @"Maintenance", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"84", @"Card Name" : @"Plus Power", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"85", @"Card Name" : @"Pokémon Center", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"86", @"Card Name" : @"Pokémon Flute", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"87", @"Card Name" : @"Pokédex", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"88", @"Card Name" : @"Professor Oak", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"89", @"Card Name" : @"Revive", @"Type" : @"Trainer", @"Rarity" : @"♦" }];
[BaseSet addObject:@{ @"No." : @"90", @"Card Name" : @"Super Potion", @"Type" : @"Trainer", @"Rarity" : @"♦" }];

[BaseSet addObject:@{ @"No." : @"91", @"Card Name" : @"Bill", @"Type" : @"Trainer", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"92", @"Card Name" : @"Energy Removal", @"Type" : @"Trainer", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"93", @"Card Name" : @"Gust Of Wind", @"Type" : @"Trainer", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"94", @"Card Name" : @"Potion", @"Type" : @"Trainer", @"Rarity" : @"●" }];
[BaseSet addObject:@{ @"No." : @"95", @"Card Name" : @"Switch", @"Type" : @"Trainer", @"Rarity" : @"●" }];

[BaseSet addObject:@{ @"No." : @"96", @"Card Name" : @"Double Colorless Energy", @"Type" : @"Energy", @"Rarity" : @"♦" }];

[BaseSet addObject:@{ @"No." : @"97", @"Card Name" : @"Fighting Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];
[BaseSet addObject:@{ @"No." : @"98", @"Card Name" : @"Fire Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];
[BaseSet addObject:@{ @"No." : @"99", @"Card Name" : @"Grass Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];
[BaseSet addObject:@{ @"No." : @"100", @"Card Name" : @"Lightning Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];
[BaseSet addObject:@{ @"No." : @"101", @"Card Name" : @"Psychic Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];
[BaseSet addObject:@{ @"No." : @"102", @"Card Name" : @"Water Energy", @"Type" : @"Energy", @"Rarity" : @"--" }];

}


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return BaseSet.count;

}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return [[BaseSet objectAtIndex:row]objectForKey:tableColumn.identifier];

}

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize managedObjectContext = _managedObjectContext;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  • http://stackoverflow.com/questions/7433495/nstableview-with-checkbox-cell use this link may be helpful for you – square Oct 21 '13 at 04:52

3 Answers3

0

I have dragged the button onto the .xib but the check marks do not click or unclick.

Drag and drop Check Box Cell to the table column to make all the cells underneath as check cells.

Check Box Cell

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{

    if([[tableColumn identifier] isEqualToString:@"CheckBoxIdentifier"])
    {
        state = model.state; // NSNumber object with int NSOnState or NSOffState
    }

    return state;
}
San
  • 1,796
  • 13
  • 22
0

you have to impement this method also

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
   if (tableView == TableView && [[tableColumn  identifier] isEqualToString:@"checkboxColumnIdentifier"])
    {

       //do code here
    }

}
PR Singh
  • 653
  • 5
  • 15
0

In case of your tableColumn which contains the check box cell, you have to return the state of your checkBox in this method

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
  {
     return [[BaseSet objectAtIndex:row]objectForKey:tableColumn.identifier];
     //In your BaseSet array add a key for state of each row that returns the state of checkbox for checkboxcelltablecolumnIdentifier. this will be the initial state of checkboxes in your column

  }

Also implement,

  - (void)tableView:(NSTableView *)theTableView setObjectValue:(id)theObject forTableColumn:(NSTableColumn *)theColumn row:(NSInteger)rowIndex
  {    
      state = [theObject boolValue];
      //[BitSet replaceObjectAtIndex:rowIndex withObject:[NSNumber numberWithInt:state]];
      // Here update your array with this new state because this method is called whenever the state of checkbox cell is changed(checkbox is clicked)
  }

As an example, I am taking a NSMutableArray arr. This will contain the states of your checkbox cells.

NSMutableArray* arr;
arr = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:NSOnState],[NSNumber numberWithInt:NSOnState],[NSNumber numberWithInt:NSOnState],[NSNumber numberWithInt:NSOnState], nil];

// Then implement data source methods as
- (void)tableView:(NSTableView *)theTableView setObjectValue:(id)theObject forTableColumn:(NSTableColumn *)theColumn row:(NSInteger)rowIndex
  {    
     state=[theObject boolValue];
     [arr replaceObjectAtIndex:rowIndex withObject:[NSNumber numberWithInt:state]]; 
  }

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
   if( [[tableColumn  identifier] isEqualToString:@"checkboxColumnIdentifier"] )
   {
       return [arr objectAtIndex: rowIndex];
   }
   return [[BaseSet objectAtIndex:row]objectForKey:tableColumn.identifier];
}
Neha
  • 1,751
  • 14
  • 36