I'm using collection view in my Mac app.
Wile entering full screen mode the first cell is getting overlapped. please find the images
I used .xib for the cell
Please find the code below. Kindly help me on this.
Thanks
DataSource:
public class CustomDeckDataSource : NSCollectionViewDataSource { List<string> deckText = new List<string>(); public CustomDeckDataSource( List<string> _deckText) { this.deckText = _deckText; ParentCollectionView = parent; playDeckView = view; } public override nint GetNumberOfSections(NSCollectionView collectionView) { return 1; } public override NSCollectionViewItem GetItem(NSCollectionView collectionView, NSIndexPath indexPath) { var item = new CustomDeckItems("CustomDeckItems", NSBundle.MainBundle); item.UpdateCell( deckText[(int)indexPath.Item]); return item; } public override nint GetNumberofItems(NSCollectionView collectionView, nint section) { return deckText.Count; } }
Cell Class:
public partial class CustomDeckItems : AppKit.NSCollectionViewItem { NSTextField test; [Weak] PlayDeckView playDeckView = null; public CustomDeckItems(IntPtr handle) : base(handle) { } // Called when created directly from a XIB file [Export("initWithCoder:")] public CustomDeckItems(NSCoder coder) : base(coder) { } public CustomDeckItems(string nibName, NSBundle nibBundle) : base(nibName, nibBundle) { } public CustomDeckItems() : base("DeckItems", NSBundle.MainBundle) { } public override void ViewDidAppear() { base.ViewDidAppear(); this.View.Layer.BorderColor = NSColor.FromRgb(126, 200, 200).CGColor; this.View.Layer.BorderWidth = 1.0f; } internal void UpdateCell( string _text) { test = new NSTextField(); test.StringValue = _text; this.View.AddSubview(test); } }
ViewController:
List<string> deckLables = new List<string>(); PlayDeckDataSource playDeckDataSource; public override void ViewDidLoad() { base.ViewDidLoad(); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); deckLables.Add("one"); this.DeckCollectionView.RegisterClassForItem(typeof(CustomDeckItems), "DeckItems"); var flowlayout = new NSCollectionViewFlowLayout() { ItemSize = new CGSize(180, 40), SectionInset = new NSEdgeInsets(5, 5, 5, 5), // ScrollDirection = NSCollectionViewScrollDirection.Vertical }; this.DeckCollectionView.CollectionViewLayout = flowlayout; customDeckDataSource = new CustomDeckDataSource(this, this.DeckCollectionView, deckLables); DeckCollectionView.DataSource = customDeckDataSource; DeckCollectionView.ReloadData(); }
The problem is happening because you keep adding control(NSTextField) into NSCollectionViewItem
.
The collecitonview may reload when you're trying to change the size of the screen , GetItem
method triggers and then UpdateCell
method triggers , you add the control again but you never remove it .
Add the control into ViewDidLoad
method and just update its text in UpdateCell
method .
public override NSCollectionViewItem GetItem(NSCollectionView collectionView, NSIndexPath indexPath) { var item = collectionView.MakeItem("DeckItems",indexPath) as CustomDeckItems ; item.UpdateCell( deckText[(int)indexPath.Item]); return item; }
public override void ViewDidLoad() { base.ViewDidLoad(); test = new NSTextField(); this.View.AddSubview(test); //only add once . } internal void UpdateCell( string _text) { test.StringValue = _text; }
Refer to official sample
https://docs.microsoft.com/en-us/samples/xamarin/mac-samples/maccollectionnew/.
Answers
The problem is happening because you keep adding control(NSTextField) into
NSCollectionViewItem
.The collecitonview may reload when you're trying to change the size of the screen ,
GetItem
method triggers and thenUpdateCell
method triggers , you add the control again but you never remove it .Solution
Add the control into
ViewDidLoad
method and just update its text inUpdateCell
method .CustomDeckDataSource
CustomDeckItems
Refer to official sample
https://docs.microsoft.com/en-us/samples/xamarin/mac-samples/maccollectionnew/.