Search code examples
objective-cmacoscocoa-bindings

NSOutlineview with checkbox


I am looking to add checkboxes to NSOutlineview with bindings using as the basis this project https://developer.apple.com/library/archive/samplecode/SourceView/Introduction/Intro.html

How do I add the behavour to allow users whereby if I click a parent checkbox, then it will select the children, and if I unclick it - it will deselect the children of that item?

Thanks in advance.


Solution

  • Connect the action of the checkbox to an action method of the delegate of the outline view. In the action method, set the property of the child nodes and reload the item and children.

    - (IBAction)checkboxAction:(id)sender {
        NSInteger row = [self.myOutlineView rowForView:sender];
        if (row >= 0) {
            id item = [self.myOutlineView itemAtRow:row];
            if ([item isKindOfClass:[NSTreeNode class]]) {
                BaseNode *node = [item representedObject];
                if ([node isKindOfClass:[BaseNode class]]) {
                    for (BaseNode *child in node.children)
                        child.isSelected = node.isSelected;
                    [self.myOutlineView reloadItem:item reloadChildren:YES];
                }
            }
        }
    }