2013-10-20 62 views
1

我應該開始說我對xcode非常陌生。我覺得這很有趣,並且明白,如果你對我在這個領域的不敏感感到惱火。但是,如果你覺得你可以幫助我,我會非常感激。現在,我創建了一個包含6列的NSTable,並使用文本成功地填充了它的單元格。現在我想取最後2個單元格,並將它們複選框單元格。我已將按鈕拖到.xib上,但複選標記不會單擊或不點擊。所以我的問題是......我如何使這些複選標記正常工作?NSTable上的複選框單元格

這是我的.h代碼

#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 

這是我.M碼。

#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 
{ 
+0

http://stackoverflow.com/questions/7433495/nstableview-with-checkbox-cell使用這個鏈接 – square

回答

0

在你的TableColumn其中包含的複選框電池的情況下,你必須回到這個方法您的複選框的狀態

- (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 

    } 

還執行,

- (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) 
    } 

作爲一個例子,我正在使用一個NSMutableArray arr。這將包含您的複選框單元格的狀態。

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]; 
} 
+0

感謝所有幫助傢伙可能對你有所幫助。我使用你的提示工作。 –

0

我已經拖了按鈕到的.xib但對號不要點擊或取消勾選。

拖放複選框細胞表列,使所有單元格下方的檢查細胞。

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; 
} 
0

你必須impement這種方法也

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

     //do code here 
    } 

} 
相關問題