Hope this helps you !!!
Customcontrol
GridCollectionView.cs using System; using CoreGraphics; using Foundation; using UIKit; namespace MyApp.Forms.Controls { public class GridCollectionView : UICollectionView { public GridCollectionView () : this (default(CGRect)) { } public GridCollectionView(CGRect frm) : base(frm, new UICollectionViewFlowLayout()) { AutoresizingMask = UIViewAutoresizing.All; ContentMode = UIViewContentMode.ScaleToFill; RegisterClassForCell(typeof(GridViewCell), new NSString (GridViewCell.Key)); } public bool SelectionEnable { get; set; } public double RowSpacing { get { return ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumLineSpacing; } set { ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumLineSpacing = (nfloat)value; } } public double ColumnSpacing { get { return ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumInteritemSpacing; } set { ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumInteritemSpacing = (nfloat)value; } } public CGSize ItemSize { get { return ((UICollectionViewFlowLayout)this.CollectionViewLayout).ItemSize; } set { ((UICollectionViewFlowLayout)this.CollectionViewLayout).ItemSize = value; } } public override UICollectionViewCell CellForItem(NSIndexPath indexPath) { if (indexPath == null) {
Renderer class
Gridviewrenderer.cs using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using Foundation; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; using MyApp.Controls; using System.Collections.Generic; [assembly: ExportRenderer (typeof(GridView), typeof(GridViewRenderer))] namespace MyApp.Controls { public class GridViewRenderer: ViewRenderer<GridView,GridCollectionView> { private GridDataSource _dataSource; public GridViewRenderer () { } public int RowsInSection(UICollectionView collectionView, nint section) { return ((ICollection) this.Element.ItemsSource).Count; } public void ItemSelected(UICollectionView tableView, NSIndexPath indexPath) { var item = this.Element.ItemsSource.Cast<object>().ElementAt(indexPath.Row); this.Element.InvokeItemSelectedEvent(this, item); } public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) { var item = this.Element.ItemsSource.Cast<object>().ElementAt(indexPath.Row); var viewCellBinded = (this.Element.ItemTemplate.CreateContent() as ViewCell); if (viewCellBinded == null) return null; viewCellBinded.BindingContext = item; return this.GetCell(collectionView, viewCellBinded, indexPath); } protected virtual UICollectionViewCell GetCell(UICollectionView collectionView, ViewCell item, NSIndexPath indexPath) { var collectionCell = collectionView.DequeueReusableCell(new NSString(GridViewCell.Key), indexPath) as GridViewCell; if (collectionCell == null) return null; collectionCell.ViewCell = item; return collectionCell; } protected override void OnElementChanged (ElementChangedEventArgs<GridView> e) { base.OnElementChanged (e); if (e.OldElement != null) { Unbind (e.OldElement); } if (e.NewElement != null) { if (Control == null) { var collectionView = new GridCollectionView() { AllowsMultipleSelection = false, SelectionEnable = e.NewElement.SelectionEnabled, ContentInset = new UIEdgeInsets ((float)this.Element.Padding.Top, (float)this.Element.Padding.Left, (float)this.Element.Padding.Bottom, (float)this.Element.Padding.Right), BackgroundColor = this.Element.BackgroundColor.ToUIColor (), ItemSize = new CoreGraphics.CGSize ((float)this.Element.ItemWidth, (float)this.Element.ItemHeight), RowSpacing = this.Element.RowSpacing, ColumnSpacing = this.Element.ColumnSpacing }; Bind (e.NewElement); collectionView.Source = this.DataSource;
For more information, click here for a custom class and click here for a rendering class.
source share