我们如何从OS X中的NSTableView中删除额外的行?

时间:2017-08-29 03:49:39

标签: objective-c macos nstableview nstablecellview

我需要从NSTableView和NSOutlineView中删除未使用的单元格行。在UITableView中,我们有tablefooter的属性。有没有NSTableView的东西。

请检查代码及其相关说明。

dataArr是一个包含SearchItem类型对象的数组,它们只是数据的模态。 dataArr有4个对象意味着NSOutline视图中将出现总共4行。

((SearchItems *)item)。children是与单个SearchItems对应的OutlineView子项的数据数组。每行有2个孩子,所以当你展开行时,会有2个孩子出现。

//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 个答案:

答案 0 :(得分:0)

使用此委托方法返回自定义行。这样,您可以按照示例进行精美的绘制和间隔。请注意,我尚未解决的顶层箭头上方的小绘图故障。

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

enter image description here