locked
NSCollectionView 10.11 API ? RRS feed

  • Question

  • User33830 posted

    I am trying to get a small sample working using the new NSCollectionView 10.11 API trying to use the flow layout,

    I have views loaded and content appeared to filled in but they appear on the top of each other and have no clue to why ?

    All Views have the position of 0,0 however the scrollview appears to be correct and when changing size the values change as if they were off the page ?

    Any thoughts to where I am going wrong ?

    SampleProject Attached

    `

    public sealed class DateControllerScroller : NSScrollView
    {
        public DateControllerScroller (RectangleF aFrame): base(aFrame)
        {
            this.HasVerticalScroller = true;
            this.HasHorizontalScroller = true;
            this.AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.HeightSizable;
            this.AutoresizesSubviews = true;
            NSCollectionView m_aCollectionView = new NSCollectionView ();
            m_aCollectionView.Frame = aFrame;
            m_aCollectionView.DataSource = new DateControllerCollectionViewSource();
            m_aCollectionView.CollectionViewLayout = new NSCollectionViewFlowLayout()
            {
                ItemSize = new SizeF(200,200),
                MinimumLineSpacing = 10,
                MinimumInteritemSpacing =10,
                ScrollDirection = NSCollectionViewScrollDirection.Vertical
            };
    
            this.DocumentView = m_aCollectionView;
        }
    }
    
    public class DateControllerCollectionViewSource : NSCollectionViewDataSource
    {
        NSObject[] SampleValues = new NSObject[] { 
            new NSString("Item 1"),
            new NSString("Item 2"),
            new NSString("Item 3"),
            new NSString("Item 4"),
            new NSString("Item 5"),
            new NSString("Item 6")
        };
    
        public override NSCollectionViewItem GetItem(NSCollectionView collectionView, NSIndexPath indexPath)
        {
            return new CollectionViewItem()
            {
                RepresentedObject = SampleValues[indexPath.Item]
            };
        }
        public override int GetNumberofItems(NSCollectionView collectionView, int section)
        {
            return SampleValues.Length;
        }
    
        public override int GetNumberOfSections(NSCollectionView collectionView)
        {
            return 1;
        }
    }
    
    public class CollectionViewItem : NSCollectionViewItem
    {
        public CollectionViewItem () : base ()
        {
            this.View = new DateView( new RectangleF(0,0,200, 200));
        }
    
        public override NSObject RepresentedObject
        {
            get {return base.RepresentedObject; }
            set
            {
                if (value != null)
                {
                    base.RepresentedObject = value;
                    NSString aString = value as NSString;
                    if (aString == null)
                    {
                        return;
                    }
                    ((DateView)this.View).Title = aString.ToString();
                }
            }
        }
    
        public override bool Selected
        {
            get
            {
                return base.Selected;
            }
            set
            {
                base.Selected = value;
                ((DateView)this.View).Selected = value;
            }
        }
    }
    

    `

    Friday, November 27, 2015 5:10 PM

Answers

  • User1208 posted

    I have also been suffering with this.

    The NSCollectionView Class Reference doesn't seem to of been updated yet, but there are some hints in the OS X 10.11 release notes: https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/

    However the only way I coud get it to work was the NIB-based approach exactly replicating the method in the WWDC sample: https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection/Introduction/Intro.html

    Good luck!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 3, 2015 8:19 PM

All replies

  • User35201 posted

    I'm not sure if you've seen the documentation we have on NSCollectionView:

    http://developer.xamarin.com/guides/mac/user-interface/working-with-collection-views/

    though it does everything in bindings.

    I did a bit of poking around.

    You can often debug layout issues by setting something like this in every view in question:

    view.WantsLayer = true;
    view.Layer.BackgroundColor = new CGColor (0, 0, 1); // Different color for each
    

    I believe the problem is that you are overwriting the View in NSCollectionViewItem.

    From reading the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSCollectionViewClass/index.html#//appleref/occ/instp/NSCollectionView/itemPrototype), if you want to do everything in code, I believe you need to set the ItemPrototype if you want to have a custom view in your collection.

    Monday, November 30, 2015 4:53 PM
  • User1208 posted

    I have also been suffering with this.

    The NSCollectionView Class Reference doesn't seem to of been updated yet, but there are some hints in the OS X 10.11 release notes: https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/

    However the only way I coud get it to work was the NIB-based approach exactly replicating the method in the WWDC sample: https://developer.apple.com/library/prerelease/mac/samplecode/CocoaSlideCollection/Introduction/Intro.html

    Good luck!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 3, 2015 8:19 PM
  • User33830 posted

    Thanks @SteveParker for the info.

    @ChrisHamons In the new API ItemPrototype is pretty much replaced by NSCollectionViewDataSource so you can have different types of NSCollectionViewItems

    I will get a small sample together and post back here

    Thursday, December 10, 2015 2:55 PM
  • User31231 posted

    All,

    I'm in the process of updating all of the Xamarin.Mac documentation (and its related samples) to use storyboards. Collection Views and Data Binding is next on the list so I hope to have it ready soon.

    Thursday, December 10, 2015 9:46 PM
  • User4114 posted

    Hi, has there been any progress? I see the Mac guide still refers to the old data binding method.

    I came across this article because I am attempting to use the new 10.11 NSCollectionViewDataSource technique, and I'm getting the same result as @DavidLilly.

    Thursday, March 23, 2017 7:41 PM
  • User31231 posted

    Hi @AdamLangley.8922,

    I've been working on a new guide but have been pulled off onto other tasks and haven't had a chance to finish it yet. Sorry this has taken so long.

    If you don't mind doing a bit of transcoding yourself, I found this excellent post on the topic: Collection Views in OS X Tutorial.

    Friday, March 24, 2017 12:58 PM