Sectioned UICollectionView with MVVMCross

Łukasz Ławicki
Feb 18 · 4 min read

Recently I was asked to implement CollectionView with sections on Xamarin.iOS. In that project, we were using MVVMCross. I knew there is an option to do it on iOS native, so I thought of a solution on how to do it in Xamarin. Unfortunately, I didn’t find a sample code so I had to figure it out on my own 😢

In this article, I will show you how to implement Sectioned UICollectionView on Xamarin. Although, in the sample code, I am using MVVMCross to handle my bindings etc. this doesn’t mean that it works only for this framework. It’s cross framework one ;-)

For the purpose of this tutorial, I am going to create a list of books. There will be several categories and each category can contain a different amount of books. Let’s go!

If you came only for the code, at the end of this article you can find a link to sample repository on Github.

Disclaimer

One more thing before we start: I don’t think this solution is the perfect one. It was constructed based on SWIFT code, StackOverflow answers and other small hints. I’ve just put it all together and made a sample.

To make my UI nicer I added FFImageLoading library, so I can show books thumbnails.

Step by step solution

Having an introduction let’s proceed with some coding. Firstly, create your UICollectionView and add it to your View/page. Basically, this step truly does not matter — you simply need to have UICollectionView added.

In the next step (secondly) I’d suggest you should create your ItemCells & HeaderCell. In order to create your ItemCells, you need to create a class that derives from MvxCollectionViewCell . You can do it by either creating a XIB file or .cs file. Populate the view, create the bindings and your are done. Mine cell looks like this

As you can see I am doing everything in code. Three of my elements are bindable and that’s all. Not that complex. Let’s proceed to the HeaderView. Similar to the ItemCell you need to create a class that inherits from UICollectionReusableView. Again, either create a XIB or .cs file. I will proceed with .cs file. Here is what I have:

This one is even simpler than the cell itself. The header only contains text. Pff. Easy. By now, we have created UICollectionView, ItemCells & HeaderView. In order to combine everything, we need to implement CollectionViewSource. Let’s talk about it in the next paragraph.

CollectionViewSource with header

Normally if we want to populate the TableView of CollectionView with a simple list we need to bind to ItemsSource. This time it is different. We can’t simply use ItemsSource as we need to combine the Items with the headers. In order to it I’ve created a class SectionItem — it has two properties: Header object (in my case HeaderText) and collection.

In my ViewModel I have

which contains Sections with Items.

The most interesting part it the logic behind CollectionViewSource. Let me explain it by example.

Although I hope this code is pretty obvious, I’d like to put a stress on the following methods: NumberOfSections, GetItemsCount, GetItemAt, GetViewForSupplementaryElement, GetOrCreateCellFor.

I won’t get into many details as this is well documented on Apple’s pages but those are the methods that are responsible for making our SectionedCollectionView work.

A! I’ve almost forgotten: don’t forget to register your header by callingRegisterClassForSupplementaryView on your Collection!

Conclusion

The code above lets you achieve this kind of a result.

As you can see we have a nice CollectionView divided into sections. This is what we were supposed to achieve and this is what was achieved! Perfect! 😇

CollectionViews combined with FlowLayout are powerful controls on iOS. Adding headers and footers gives them extra powers. I always like to see them when using an iPad or bigger iPhone screens. They look nicer then TableViews, they perform good and they are really user-friendly.

I really hope that someone will find this post helpful. As I have stated in the introduction, I didn’t find a similar article on the web so I decided to write one on my own.

If you found any mistakes or something is unclear, please don’t hesitate to ask me by writing a comment. If you found this article useful, please show me some love and click the clap button below. It keeps me motivated!


Sample repository available on Github.

Originally posted on my blog 😇

Łukasz Ławicki

Written by

Xamarin developer from Wroclaw, PL

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade