Creating Collection View Cells from a Xib

I’ve had the question of how to create a collection view cell using a xib file come up a few times, so I figured I would put together a quick example to show how to do it. In this case, I just created a collection view with 100 cells each containing a label:


Basically, you need to register the cell using the CollectionView.RegisterNibForCell method like this:

CollectionView.RegisterNibForCell (UINib.FromName ("CVCell", NSBundle.MainBundle),

There is a template file in Xamarin Studio you can use to create a collection view cell. This will create a xib to design the cell in, along with a class that is wired up to the xib. The template adds a bit of additional code but all you really need is the constructor that takes an IntPtr, as the class will be created by iOS via the call to DequeueReusableCell.

For example, here’s the code for the cell I’m using in this simple example:

public partial class CVCell : UICollectionViewCell
    public string Text {
            Label.Text = value;

    public CVCell (IntPtr handle) : base (handle)

The Text property simply sets the text on a UILabel that is added in Interface Builder.

The code to create the cell in the UIViewController is just like when you create the cell entirely in code:

public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
    var cell = (CVCell)collectionView.DequeueReusableCell (cellId, indexPath);

    cell.Text = indexPath.Row.ToString ();

    return cell;

The project is available in my github repo here.

Thoughts on MonoTouch

As you likely have heard, Attachmate let go of the entire Mono team recently, including all the outstanding people that created and supported MonoTouch. This came as a shock to many, including me, for a variety if reasons:

• There are many existing customers of MonoTouch.
• The community around MonoTouch loves the product.
• There have been some nice success stories recently of apps built with MonoTouch.
• The team that developed MonoTouch and Mono for Android are top-shelf developers.
• Several new books were near release for MonoTouch and Mono for Android.

I got involved with MonoTouch at the beginning of the first beta. I had already been doing some iPhone work with Apple’s tool chain and was (and still am) quite happy with it, but since I have always liked the C# language and .NET, I thought this would be an interesting technology to try out. Initially I was mainly intrigued with the fact that they could even create such a product from outside of Apple. As I learned more about it, I was nothing but impressed with how nicely they blended the C# language with CocoaTouch. All the knowledge I had from the Objective-C side was totally applicable, but I suddenly could bring other technologies to the table, such as being able to take advantage of nice C# language features like Linq, the ability to reuse non-UI code and additional library support via Mono’s implementation of the .NET framework class libraries.

As the technology builds on Apple’s stack, I didn’t have to trade away platform capabilities in any way. At the same time, I found a vibrant community of like-minded developers both from the Mono team as well as outside. I was having such a good time with the technology and the community that I decided to take some time to write about it a little bit on this blog, which eventually led to a book project with Addison-Wesley. I couldn’t have been more excited.

Then, in April of last year, Apple introduced licensing changes that effectively prohibited the use of technologies such as MonoTouch. Although in practice nothing ever got rejected for being built with MonoTouch, the looming threat of this resulted in my book project being placed on hold. I kept doing MonoTouch on my own, because I truly enjoy using it and participating in the community. However, business interests that I was doing MonoTouch development for, quite reasonably, could not proceed with such ambiguity, resulting in my moving some work over to Objective-C.

As you may imagine, I was pretty disappointed by all this, but then late in the summer, Apple changed the licensing again and everything was set back in motion. Excited as ever, I turned my attention back to reworking the parts of the book I had already written for the changes that came out over the time I was in limbo, and proceeded working on the remainder of the book. This work took me up to the end of last year, with the technical review following earlier this year. The book has entered the production process with my publisher and I’m proud of the result and grateful for all the fine people that helped me along the way. My publisher was planning a summer release, which would put the book out around the time of the upcoming Monospace conference. Things were looking great.

Then all these great Mono developers got laid off. First and foremost, I feel awful for any of the great people on the Mono team. They do spectacular work and provide amazing passion with everything they do. Such a team should have a better fate. As it turns out they just may.

Never one to give up so easily, not in the face of licensing issues and not even now with the entire team being dismissed, Miguel and his team are spinning up a new company, Xamarin, to continue what they started. He announced, among other things, that they would be bringing a new iOS product to the market that will be compatible with MonoTouch. This leads me to the current plan for my book.

UPDATE: My publisher has decided to send the book to print. It will be in available in bookstores by late July.

After discussing all the options with my publisher we have decided to go forward with the book in electronic form for now. We will be holding back from doing a print copy until more details emerge regarding Xamarin’s new offering. If we were to go ahead and print the book now it would be, well, printed. By sticking with an eBook we can make the information available to the many existing people using MonoTouch and still potentially offer an updated version for the new product in the future. I’m personally looking forward with great excitement to see what Miguel and his team create next.