Attach NSTableCellView to TableColumn programmatically

Hi there, I'm an experienced c# developer porting an app to the Mac/iOS although I'm very new to writing anything for an Apple environment so it's a massive learning curve finding out how it all hangs together. I have successfully used the storyboard to get a TableView populated with my data and displaying. However I've got about 20-30 of these tableview to add and it seemed pointless to manually configure them all when they are all the same general idea just different interfaces describing the display fields from different classes.

So instead of using storyboards I want to create my viewcontroller/tablesource programatically and once it's working for a test case I can eventually move on to using a generic class to create a controller/tablesource. NB. I'm using a tablesource in order to make the code differences between a MacOS version and an iOS version minimal.

My issue is that when I create a table column it's not got a TableCellView and try as I might I cannot find a way of programmatically adding an NSTableCellView (standard or customised) to my columns. Here is my code.

ViewController:

using System;
using Foundation;
using AppKit;
using FTAnalyzer.Mac.DataSources;
using System.Reflection;

namespace FTAnalyzer.Mac
{
    public partial class IndividualsViewController : NSViewController
    {
        FamilyTree _familyTree;
        NSTableView _individualsTableView;

        public IndividualsViewController(IntPtr handle) : base(handle)
        {
            _familyTree = FamilyTree.Instance;
        }

        public void ResetDocument()
        {
            if (!NSThread.IsMain)
            {
                InvokeOnMainThread(() => ResetDocument());
                return;
            }

            _individualsTableView = new NSTableView()
            {
                RowSizeStyle = NSTableViewRowSizeStyle.Default,
                Enabled = true,
                UsesAlternatingRowBackgroundColors = true,
                ColumnAutoresizingStyle = NSTableViewColumnAutoresizingStyle.Sequential,
                Source = new IndividualsTableSource()
            };

            PropertyInfo[] properties = typeof(IDisplayIndividual).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                var tableColumn = new NSTableColumn()
                {
                    Identifier = property.Name,
                    Width = 100,
                    Editable = false,
                    Hidden = false,
                    Title = property.Name
                };
                _individualsTableView.AddColumn(tableColumn);
            }
        }

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

            ResetDocument();
            _individualsTableView.ReloadData();

            var scrollView = new NSScrollView()
            {
                DocumentView = _individualsTableView
            };
            View = scrollView;
        }
    }
}

TableSource:

using System;
using Foundation;
using AppKit;
using FTAnalyzer.Mac.DataSources;
using System.Reflection;

namespace FTAnalyzer.Mac
{
    public partial class IndividualsViewController : NSViewController
    {
        FamilyTree _familyTree;
        NSTableView _individualsTableView;

        public IndividualsViewController(IntPtr handle) : base(handle)
        {
            _familyTree = FamilyTree.Instance;
        }

        public void ResetDocument()
        {
            if (!NSThread.IsMain)
            {
                InvokeOnMainThread(() => ResetDocument());
                return;
            }

            _individualsTableView = new NSTableView()
            {
                RowSizeStyle = NSTableViewRowSizeStyle.Default,
                Enabled = true,
                UsesAlternatingRowBackgroundColors = true,
                ColumnAutoresizingStyle = NSTableViewColumnAutoresizingStyle.Sequential,
                Source = new IndividualsTableSource()
            };

            PropertyInfo[] properties = typeof(IDisplayIndividual).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                var tableColumn = new NSTableColumn()
                {
                    Identifier = property.Name,
                    Width = 100,
                    Editable = false,
                    Hidden = false,
                    Title = property.Name
                };
                _individualsTableView.AddColumn(tableColumn);
            }
        }

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

            ResetDocument();
            _individualsTableView.ReloadData();

            var scrollView = new NSScrollView()
            {
                DocumentView = _individualsTableView
            };
            View = scrollView;
        }
    }
}
Tagged:

Best Answer

  • ShammyLevvaShammyLevva ✭✭
    Accepted Answer

    Ok forget this. The solution was that I was creating a view on load then when a document was opened it was accidentally calling a new view controller overwriting the one originally created. So the one attached to the scroll view wasn't the one getting populated nothing to do with NSTableView/NSTableCellView.

Answers

  • ShammyLevvaShammyLevva Member ✭✭
    Accepted Answer

    Ok forget this. The solution was that I was creating a view on load then when a document was opened it was accidentally calling a new view controller overwriting the one originally created. So the one attached to the scroll view wasn't the one getting populated nothing to do with NSTableView/NSTableCellView.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Welcome to the Xamarin.Mac forums. It sounds like you've sorted out your issue. Rubber Duck Debugging is often effective.

    If you'd like some documentation \ samples to dig more into Xamarin.Mac, may I suggest the items listed here:
    https://forums.xamarin.com/discussion/comment/306182/#Comment_306182

Sign In or Register to comment.