Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Attach NSTableCellView to TableColumn programmatically

$
0
0

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;
        }
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>