Var cell = collectionView.DequeueReusableCell( Public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) Public ShelvesCollectionViewDataSource(DataProvider dataProvider) Private readonly DataProvider dataProvider Public class ShelvesCollectionViewDataSource : UICollectionViewDataSource In our case we want the shelf to fill the width of it’s parent collectionview. The ShelvesCollectionViewDelegate is just used to return the size of the collection view cell. The GetItemsCount method uses the DataProvider.Shelves property to return the correct number of shelves available. Because this shelf also has a UICollectionView I created helper classes to implement the collection view datasource and delegate for this. In GetCell we instantiate a cell (which will be a single shelf) using DequeueReusableCell and cast the result as a ShelfViewCell. The ShelvesCollectionViewDataSource implements the UICollectionViewDataSource methods GetCell and GetItemsCount. (UINib.FromName(nameof(ShelfViewCell), null), CellIdentifier) If ( is UICollectionViewFlowLayout flowLayout)įlowLayout.MinimumLineSpacing = (System.nfloat) 40.0 = new ShelvesCollectionViewDataSource(this.shelvesDataProvider) = new ShelvesCollectionViewDelegate(this) Public ViewController(IntPtr handle) : base(handle)Īwait shelvesDataProvider.LoadShelvesAsync() Public static readonly string CellIdentifier = "ShelfViewCellIdentifier" Private DataProvider shelvesDataProvider = new DataProvider() Public partial class ViewController : UIViewController As the UICollectionViewCell is defined in a xib (rather than in the storyboard) the cell has to be registered using the method RegisterNibForCell. In the ViewDidLoad method I’m creating instances of the ShelvesCollectionViewDelegate and ShelvesCollectionViewData source classes. In C# you can only inherit from one base class so that necessitates creating helper classes that implement these. As there are no protocols in C# the UICollectionViewDataSource and UICollectionViewDelegate protocols for supporting Collection Views are defined as abstract base classes. This will act as the collection of all shelves. In the main ViewController (created by Visual Studio when creating a project) I’ve added a single UICollectionView to the storyboard, set it to flow vertically (the individual shelves by contrast will flow horizontally), and constrained it to fill most of the screen. It’s also asynchronous just to reflect that this would likely involve a network request in the real world. The DataProvider provides a single method, LoadShelvesAsync, which populates the list of shelves in the Shelves property. To start with I created a simple DataProvider class which just returns a list of simple Shelf objects, and each one of these containing a list of ShelfItems, each of which contains an image URL. In implementing this example it’s reminded me how much of a frustrating experience it still is developing Xamarin iOS in Visual Studio on either Windows or Mac. There is no databinding easily available. But with Xamarin iOS it’s not quite as straight forward. With XAML I think this could be achieved with relatively little code using a ListView within a ListView, binding to a ViewModel. A common layout for appleTV or other media / TV apps is to present content as a vertical list of shelves (or rails) of data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |