Unable to show CheckBox and ComboBox controls in NSTableView

AceRaviAceRavi INMember
edited November 2016 in Xamarin.iOS

Hi,

I am trying to create a simple Mac App using Xamarin.

I have a NSTableView control that has 3 columns in it. The first column is a CheckBox type, second column is a label, third column is a ComboBox type. Following image show the App design.

image

Here is my code:

public class Mapping
{
    #region Computed Propoperties
    public NSButtonCell SelectCell { get; set; } = null;
    public string MySQLColumnName { get; set; } = "";
    public NSComboBoxCell ExcelColumns { get; set; } = null;
    #endregion

    #region Constructors
    public Mapping()
    {
    }

    public Mapping(NSButtonCell select, string mysqlcolumnname, NSComboBoxCell excelcolumns)
    {
        this.SelectCell = select;
        this.MySQLColumnName = mysqlcolumnname;
        this.ExcelColumns = excelcolumns;
    }
    #endregion
}

public class MappingTableDataSource : NSTableViewDataSource
{
    #region Public Variables
    public List<Mapping> Mappings = new List<Mapping>();
    #endregion

    #region Constructors
    public MappingTableDataSource()
    {
    }
    #endregion

    #region Override Methods
    public override nint GetRowCount(NSTableView tableView)
    {
        return Mappings.Count;
    }
    #endregion
}



public class MappingTableDelegate : NSTableViewDelegate
{
    #region Constants 
    private const string CellIdentifier = "ProdCell";
    #endregion

    #region Private Variables
    private MappingTableDataSource DataSource;
    #endregion

    #region Constructors
    public MappingTableDelegate(MappingTableDataSource datasource)
    {
        this.DataSource = datasource;
    }
    #endregion

    #region Override Methods
    public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row)
    {

        NSTextField view = (NSTextField)tableView.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;
        }

        switch (tableColumn.Title)
        {
            case "Select":
                view = DataSource.Mappings[(int)row].SelectCell;
                break;
            case "MySQL Column":
                view.StringValue = DataSource.Mappings[(int)row].MySQLColumnName;
                break;
            case "Excel Column":
                view = DataSource.Mappings[(int)row].ExcelColumns;
                break;

        } 

and on a click of a push button-

    partial void clickImportFromExcel(NSObject sender)
    {
        var DataSource = new MappingTableDataSource();
        DataSource.Mappings.Add(new Mapping(new NSButtonCell(), "Testing Text 1", new NSComboBoxCell()));
        DataSource.Mappings.Add(new Mapping(new NSButtonCell(), "Testing Text 2", new NSComboBoxCell()));
        DataSource.Mappings.Add(new Mapping(new NSButtonCell(), "Testing Text 3", new NSComboBoxCell()));

        tblColumnMapping.DataSource = DataSource;
        tblColumnMapping.Delegate = new MappingTableDelegate(DataSource);
    }

I am getting following compile time error-

"Cannot implicitly convert type AppKit.NSButtionCell to AppKit.NSTextField"

Can someone help?

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    So a full stack trace would be very helpful in tracking things down (are you hitting https://bugzilla.xamarin.com/show_bug.cgi?id=45742 and need to add "--marshal-managed-exceptions=disable" to your additional mmp args to get a better trace)?

    Ignoring that, the only cast I see in your code is in:

        NSTextField view = (NSTextField)tableView.MakeView(CellIdentifier, this);
    

    So maybe you are getting back a different view that expected. You could try not casting there, taking the NSObject and Console.WriteLine'ing before a later cast and see what's up.

    But seriously, try to get a good trace. It should make it pretty straightforward.

Sign In or Register to comment.