NSTableView.SelectionDidChange event is not triggered when a row of a table view is selected

blagovestblagovest Member ✭✭

Hi,

As a newbie I went through the Xamarin.Mac documentation and I was able to understand how the binding and setup of a table view works. One of the things not mentioned in the documentation is how the tableview events are handled. I instantiated my tableview outlet and then call the object event SelectionDidChange. It never triggered when I single click or double click a row in the table view. What is the proper way to achieve this?

Tagged:

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Welcome to the board.

    On questions like this, posting the relevant code or attaching a sample will greatly increase your chance of getting an answer.

    NSTableView (and NSOutlineView) are pretty non-trivial to get right the first time, and the Apple documentation is less than ideal.

    Here are a few places to look for more information:

    Searching for macOS information can be challenging, I often find adding "Cocoa" to it improves results.

    For example "Cocoa NSTableView selection changed" gives https://developer.apple.com/documentation/appkit/nstableviewdelegate/1528567-tableviewselectiondidchange?language=objc which is the delegate method you need.

    But before any of those, as you are likely a new Xamarin.Mac user, I would strongly suggest reading through our FAQ:

    https://forums.xamarin.com/discussion/31680/welcome-to-the-xamarin-mac-forums-and-faq#latest

    as it links helpful documentation on how to read Objective-C documentation and other required skills.

  • blagovestblagovest Member ✭✭

    Hi Chris,

    thanks for taking the time to reply. I ma really confused as you said because of the scarce information and examples. Then you have the separation between Storyboard and xib development which makes the flow different so some of the examples are for storyboard, some of them are for xib files so I cannot really get the whole mechanism. The example you provided uses xib files but I actually develop with the storyboard pattern. Hence I am not sure I grasp the code I need to apply to my ViewController.
    So basically I am instantiating the user control and then add the event.
    The Objective C syntax doesn't give me absolutely no clue how to implement it event after adding it to the h file of the controller. Would you mind give me a code example in case of storyboard implementation?

    public override void AwakeFromNib()
            {
    
                base.AwakeFromNib();
                UsersTable = new NSTableView();
                UsersTable.SelectionDidChange += PopulateSelectedProperty;
    
                // Build list of employees
                AddPerson(new PersonModel("Craig Dunn", "Documentation Manager", true));
                AddPerson(new PersonModel("Amy Burns", "Technical Writer"));
                AddPerson(new PersonModel("Joel Martinez", "Web & Infrastructure"));
                AddPerson(new PersonModel("Kevin Mullins", "Technical Writer"));
                AddPerson(new PersonModel("Mark McLemore", "Technical Writer"));
                AddPerson(new PersonModel("Tom Opgenorth", "Technical Writer"));
                AddPerson(new PersonModel("Larry O'Brien", "API Documentation Manager", true));
                AddPerson(new PersonModel("Mike Norman", "API Documenter"));
            }
    
    
            public void PopulateSelectedProperty(object sender, EventArgs e)
            {
                selectedPerson = _people.GetItem<PersonModel>((nuint)UsersTable.SelectedRow);
            }
    
  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I'll answer your explicit questions, but I'll suggest starting with this documentation:

    https://developer.xamarin.com/guides/mac/getting_started/hello,_mac/

    There are a number of samples in general (https://developer.xamarin.com/samples/mac/all/) but they are far from complete in covering all APIs. Often you'll need to convert objective-c stack overflow / tutorials / apple documentation to C# and this documentation will help:

    https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/
    https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/

    Moving on from that:

  • blagovestblagovest Member ✭✭

    Hi Chris,

    you will excuse my dumbness but I am not able to get the syntax I need to apply in my View Controller as the delegate notification is something I don't understand how to apply. Should I export first some of the objects I am dealing first and how the notification is applied to the function which is the delegate's variable ? Would you mind giving me a code example? I am begin you to not throw me some link - go and read. Coming from C# (having 7 months of experience) you perhaps can understand the position I am at. I don't think in 21 century programming should be so painful...

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I understand your desire to get moving, but at some point you will need to read some documentation, in particular how Apple's Delegate and DataSource system works.

    As I pointed out before, this is a link that has a full example, if a bit dated:

    https://github.com/xamarin/mac-samples/blob/master/NSOutlineViewAndTableView/NSOutlineViewAndTableView/NSTableViewCode/NSTableViewExample.cs

    You provide the NSTableView a delegate and data source which it will call back on to ask questions (what data should we show) and notify (item selected). Those can be interfaces or classes that derive from the base class in question.

    It is not trivial to provide an "example" of it using storyboards, as you have to configure the item in the storyboard over many steps. I would point you to the documentation for a walkthrough.

  • blagovestblagovest Member ✭✭
    edited June 8

    Hi Chris,

    thanks for having the time to answer me again. I should say after checking the provided the example (realised it was already downloaded on my machine) things started to get their place and their relation as well. This morning after I read your answer again your words all made sense. :)
    The issue still remains though - ShouldSelectRow seems not to be triggered when I don't have datasource defined it seems. I don't have data source here because everything is bound through XCode. Is this a normal behavior and you would suggest me to change my architecture to a xib one or I am making a mistake. Let me show you how I implemented my code:

    //setting the delegate
    public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                UsersTable = new NSTableView();
                UsersTable.Delegate = new TableDelegate();
            }
    
    
    internal class TableDelegate : NSTableViewDelegate
        {
            public override bool ShouldSelectRow(NSTableView tableView, nint row)
            {
                Console.WriteLine("SelectedRow is"+row);
                return true;
            }
        }
    

    Even using the Export syntax seems not to fix the issue i.e.

    [Export("tableView:shouldSelectRow:")]
            public override bool ShouldSelectRow(NSTableView tableView, nint row)
            {
                Console.WriteLine("SelectedRow is" + row);
                return true;
            }
    

    UPDATE:
    I downloaded the the xib databidning example and ShouldSelectRow was triggered so it seems my initial code tests were buggy. Let me make some more tests and I will let you know what is the final outcome.

Sign In or Register to comment.