开发者

Cocoa Design: How to highlight the current department of an employee in a NSCollectionView?

开发者 https://www.devze.com 2023-02-26 12:58 出处:网络
I have this core data backed database with entries like in the many employees example: An employee can belong to a department. A department has many employees.

I have this core data backed database with entries like in the many employees example: An employee can belong to a department. A department has many employees. I have a NSTableView (backed by a NSArrayController) with all the employees. I have a NSCollectionView (backed by a NSArrayController set to selection.possibleDepartments) that shows me possible departments for the selected employee.

*DDEmployee               
 name
 image
 -----
 possibleDepartments <<------ 
 selectedDepartment <<--     |
                        |    |
*DDDepartment        开发者_运维百科   |    |
 name                   |    |
 -----                  |    |
 employees          <---     |
 possibleEmployees <<--------

I want to highlight the department the employee is currently in

Simple right?

How do I know what the currently selected department is? I created a categorie of the department class (DDDepartment) that has "-(BOOL)isThisTheSelectedDepartment". In this function I call the app delegate to give me the main array controller. The main array controller gives me the selected employee. I ask the selected employee if this is his department. There is little image and its hidden property is bound to representedObject.isThisTheSelectedDepartment and negated. This does not update the NSCollectionView (obviously :P) Is there a way to do this with bindings?

Thanks


If you want to do it with bindings you can add an NSObjectController to your nib. Then on the object controller bind its content to the selection in the employee array controller with the keypath @"selectedDepartment". That object controller will always contain the department of the selected employee.

For DDDepartment add a BOOL "isSelected" property to it.

Create a subclass of NSObjectController. Set the NSObjectController you created in part 1 to have the subclass for its class. Now in the subclass override - (void)setContent:(id)content to be:

- (void)setContent:(id)content
{
   [[self content] setIsSelected:NO];  // clear the flag on the current selection
   [content setIsSelected:YES];  // set the flag on the new selection

   [super setContent:content];
}

Finally change your hidden binding to bind to the isSelected property instead of your isThisTheSelectedDepartment method. This should take care of everything for you and cause the department selection to change whenever you select an employee or change the department of an employee.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号