Link to home
Start Free TrialLog in
Avatar of Scott_Edge
Scott_Edge

asked on

How do I allow a checkbox in NSTableview to be checked and once checked run command

Xcode: 3.1.2

I have a NSTableView I am updating from a NSDictionary. The tableview has a checkbox that was added with IB. I can set the checkbox from the NSDictionary but I can't change it's status once it loads. I would like to allow for the status change and then when it does change run an action based of the key of the NSDictionary.

I am very new to Obj-c and am trying to work my way through this. I can track changes with - (void)tableView:(NSTableView *)tableView setObjectValue:(id)aData. what I can't figure out is how to capture the change update the NSDictionary and reload.

Thanks for any help.
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
			row:(int)row
{
	NSArray *keysArray = [speakerDict allKeys];
	NSArray *sortedKeysArray =
	[keysArray 
	 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	id keyAtRow = [sortedKeysArray objectAtIndex:row];
	
	if ([[tableColumn identifier] isEqualToString:@"speakerNameCell"])
	{
		return keyAtRow;    
	}
 
	return [speakerDict objectForKey:keyAtRow];
} 
 
//HAVING PROBLEM WITH FOLLOWING
 
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)aData 
   forTableColumn:(NSTableColumn *)aCol 
			  row:(int)row
{
//???????	found this code elsewhere
 
id loc_id, loc_data;
 
	
	// identify the table column
	loc_id = [aCol identifier];
//the class comes up ica for my checkbox so this part isn't run
	if ([loc_id isKindOfClass:[NSString class]])
	{
		// determine the old cell value
		loc_data = [[self speakerDict] objectForKey:loc_id];
		loc_data = [loc_data objectAtIndex:row];
		
		// compare the old cell value against the "new" value
		if (![loc_data isEqual:aData])
		{
			// update the data buffer
//the designation for this dictionary is wrong getting warning that foo might not respond to speakerDict
			[[[self speakerDict] objectForKey:loc_id]
			 replaceObjectAtIndex:row withObject:aData];
		}
	}
}

Open in new window

Avatar of Scott_Edge
Scott_Edge

ASKER

I see now isa isn't a class. I'm not sure why it's not assigning aCol.
I think I am not designating my NSDictionary correctly. I am executing an AppleScript and then passing the resulting information to my dictionary. But I think I need to add two keys for my data. The returned data is {speaker,state} right now I am creating a key for each speaker and then displaying that information but now I think I need to designate a key for speaker and a key for state. But I'm a little confused. Is there a row index on the NSDictionary to associate the speaker key and the state key? Here is my code for creating the dictionary. I know this isn't the correct way to do it but it works.
- (IBAction)getSpeakerList:(id)sender
{
	NSDictionary *scriptError = [[NSDictionary alloc] init];
	NSAppleScript * newAS =[[NSAppleScript alloc] initWithSource:@"set bigList to {}\r tell application \"Airfoil\" \r repeat with i from 1 to count of speakers \r set this_item to speaker i \r set theName to name of this_item \r set theConnected to connected of this_item \r copy theName & \"/\" & theConnected to end of bigList \r end repeat \r end tell \r bigList "];
	
	NSAppleEventDescriptor * paramList = [newAS executeAndReturnError:&scriptError];
	unsigned int count = [paramList numberOfItems];
	unsigned int i = 1;
	NSLog(@"%i",count);
	speakerDict = [NSMutableDictionary dictionary];
	
	
 
	for (i = 1; i <= count; ++i ) {
		NSAppleEventDescriptor * theDat = [paramList descriptorAtIndex:i] ;
		NSString * theSpeakerAndValue = [theDat stringValue];
		NSString * theSpeaker = [theSpeakerAndValue stringByDeletingLastPathComponent];
		NSString * theValue = [theSpeakerAndValue lastPathComponent];
		BOOL newValue;
 
		//convert our returned data to BOOL
		if ([theValue isEqualToString:@"true"])
		{newValue = YES;
		}
		else if ([theValue isEqualToString:@"false"])
		{newValue = NO;
		}
		
		//we need d as designation for BOOL or crashtastics happen.
		
		NSLog(@"Speaker: %@        is: %@ | %d",theSpeaker,theValue,newValue);
		[speakerDict setObject:[NSNumber numberWithBool:newValue] forKey:theSpeaker];
 
}
 
	NSArray *speakerlist = [speakerDict allKeys];
	NSArray *connected = [speakerDict allValues];
	NSLog(@"Speaker: %@        is: %@ ",speakerlist,connected);
 
	
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott_Edge
Scott_Edge

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial