How can we remove extra lines from NSTableView in OS X?

490 views Asked by At

I need to remove unused cells lines from the NSTableView and NSOutlineView. In UITableView we have property of tablefooter. is there anything for NSTableView.

Please check code and a little description about this.

dataArr is an array which contains SearchItem type objects, they are just modals for data nothing else. dataArr has 4 objects means total 4 rows will appear in NSOutline view.

((SearchItems *)item).children is the data array for OutlineView children corresponding to a single SearchItems. Every row has 2 children, So when you will expand the rows, 2 children will show.

//MARK: NSOutlineViewDataSource
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(nullable id)item
{
    if (item != nil && [item isKindOfClass:[SearchItems class]])
    {
        return ((SearchItems *)item).children.count;
    }

    return dataArr.count;
}


- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(nullable id)item
{
    if(item != nil && [item isKindOfClass:[SearchItems class]])
    {
        return ((SearchItems *)item).children[index];
    }

    return dataArr[index];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if(item != nil && [item isKindOfClass:[SearchItems class]])
    {
        return ((SearchItems *)item).children.count > 0;
    }

    return false;
}

//MARK: NSOutlineViewDelegate

- (nullable NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(nullable NSTableColumn *)tableColumn item:(id)item
{
    NSTableCellView *view;

    if([item isKindOfClass:[SearchSubItems class]])
    {
        view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"SearchSubItem" owner:self];
        view.textField.stringValue = ((SearchSubItems *)item).nameOfSubItem;
        [view.textField sizeToFit];
    }
    else
    {
        view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"SearchItem" owner:self];
        view.textField.stringValue = ((SearchItems *)item).nameStr;
        [view.textField sizeToFit];
    }

    return view;
}
1

There are 1 answers

0
Duncan Groenewald On

Use this delegate method to return a custom row. That way you can do fancy drawing and spacing as per the example. Note the little drawing glitch above the top level arrow that I haven't got around to fixing yet.

func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
        if let _ = item as? Section {
            return NSTableRowView()
        } else {
            return OSFixtureTableHeaderRowView()
        }
    }

enter image description here