Xcode NSOutlineView奇怪的行为

时间:2017-08-17 06:54:13

标签: c# xcode cocoa xamarin nsoutlineview

当我从我的NSOutlineView扩展项目时,所有人都按预期完成了它的工作。但是当所有这些都被扩展,并且第一个将被解除时,会发生一件奇怪的事情。

enter image description here

我实际做的只是实现数据源和代理。

public class FolderOutlineDataSource : NSOutlineViewDataSource
{
    #region Public Variables
    public List<Folder> Folders = new List<Folder>();
    #endregion

    public FolderOutlineDataSource()
    {
    }

    public override nint GetChildrenCount(NSOutlineView outlineView, NSObject item)
    {
        if (item == null)
        {
            return Folders.Count;
        }
        else
        {
            return ((Folder)item).Folders.Count;
        }

    }

    public override NSObject GetChild(NSOutlineView outlineView, nint childIndex, NSObject item)
    {
        if (item == null)
        {
            return Folders[(int)childIndex];
        }
        else
        {
            return ((Folder)item).Folders[(int)childIndex];
        }

    }

    public override bool ItemExpandable(NSOutlineView outlineView, NSObject item)
    {
        if (item == null)
        {
            return Folders[0].IsProductGroup;
        }
        else
        {
            return ((Folder)item).IsProductGroup;
        }

    }
}

public class FolderOutlineDelegate : NSOutlineViewDelegate
{

    #region Constants 
    private const string CellIdentifier = "FolderCell";
    #endregion

    #region Private Variables
    private FolderOutlineDataSource DataSource;
    #endregion

    public FolderOutlineDelegate(FolderOutlineDataSource datasource)
    {
        this.DataSource = datasource;
    }

    #region Override Methods
    public override NSView GetView(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
    {
        // This pattern allows you reuse existing views when they are no-longer in use.
        // If the returned view is null, you instance up a new view
        // If a non-null view is returned, you modify it enough to reflect the new data
        NSTextField view = (NSTextField)outlineView.MakeView(CellIdentifier, this);
        if (view == null)
        {
            view = new NSTextField();
            view.Identifier = CellIdentifier;
            view.BackgroundColor = NSColor.Clear;
            view.Bordered = false;
            view.Selectable = false;
            view.Editable = false;
        }

        // Cast item
        var folder = item as Folder;

        view.StringValue = folder.FolderName;

        /*
        // Setup view based on the column selected
        switch (tableColumn.Title)
        {
            case "Product":
                view.StringValue = product.Title;
                break;
            case "Details":
                view.StringValue = product.Description;
                break;
        }
        */

        return view;
    }
    #endregion
}

在我的MainViewController中,我做到了这一点:

public override void AwakeFromNib()
    {
        base.AwakeFromNib();

        // Create data source and populate
        var DataSource = new FolderOutlineDataSource();

        var Vegetables = new Folder("Vegetables");
        Vegetables.Folders.Add(new Folder("Cabbage"));
        Vegetables.Folders.Add(new Folder("Turnip"));
        Vegetables.Folders.Add(new Folder("Radish"));
        Vegetables.Folders.Add(new Folder("Carrot"));
        DataSource.Folders.Add(Vegetables);

        var Fruits = new Folder("Fruits");
        Fruits.Folders.Add(new Folder("Grape"));
        Fruits.Folders.Add(new Folder("Cucumber"));
        Fruits.Folders.Add(new Folder("Orange"));
        Fruits.Folders.Add(new Folder("Blackberry"));
        DataSource.Folders.Add(Fruits);

        var Meats = new Folder("Meats");
        Meats.Folders.Add(new Folder("Beef"));
        Meats.Folders.Add(new Folder("Pork"));
        Meats.Folders.Add(new Folder("Veal"));
        DataSource.Folders.Add(Meats);

        // Populate the outline
        ovFiles.DataSource = DataSource;
        ovFiles.Delegate = new FolderOutlineDelegate(DataSource);
    }

我直接从Xamarin Tutorials Page获得了此代码。 有没有人有这个东西或者我错过了什么?

感谢您的帮助!

0 个答案:

没有答案