Displaying different values for each row in NSTableView
I am trying to make a program that will have an NSTableView
that I can add and remove values from, and for each value it will store a few different variables that will be displayed in text boxes when a user selects an item from the table. I've already wrote the code to add and remove values, but can't seem to find a wa开发者_Python百科y to get the rest of the functionality to work. How can I do this?
I suggest that you you represent each element in your tableview datasource (your array of objects) as a NSDictionary. This enables you to keep several variables for each tableview item, which can be displayed in textboxes when an item is clicked. Apple has a very good example that illustrates what I think you are trying to do. Take a look at the NSTableViewBinding example. In the example, when the user double-clicks an item in the tableview an inspect method is called. You can use this method to display your variables from the dictionary in textboxes:
- (void)inspect:(NSArray *)selectedObjects
{
// this is an example of inspecting each selected object in the selection
int index;
int numItems = [selectedObjects count];
for (index = 0; index < numItems; index++)
{
NSDictionary *objectDict = [selectedObjects objectAtIndex:index];
if (objectDict != nil)
{
NSLog(@"inspect item: {%@ %@, %@}",
[objectDict valueForKey:@"firstname"],
[objectDict valueForKey:@"lastname"],
[objectDict valueForKey:@"phone"]);
[myTextBox1 setStringValue:[objectDict valueForKey:@"firstname"]];
[myTextBox2 setStringValue:[objectDict valueForKey:@"lastname"]];
}
}
}
精彩评论