Monthly Archives: January 2015

RSDF MyDayCell

Create your Custom Day Cell, MyDayCell

Let’s create a custom cell that uses all of RSDFDatePickerDayCell, but we want to add a small feature of our own. Specifically, we want to be able to add the feature of coloring a background.

RSDFDatePickerView.m changes

We make some changes like the following:

The first is to have the collection view dequeue and reuse our custom Day Cell. We do this by declaring a unique string MyDayCellIdentifier. Then in cellForItemAtIndexPath method, we hook up a custom “shouldColorDate” method for our delegate to decide whether a certain date should be colored or not.

Remember to add the nsstring for MyDayCellIdentifier at the top of the file along with the rest of the static nsstring const:

Register your custom cell class with forCellWithReuseIdentifier

In RSDFDatePickerView.m, add in your customCell class type. This is so that you can register your cell’s class type for the UICollectionView’s forCellWithReuseIdentifier method as shown below:

Whenever a UICollectionView or a UITableView uses cell classes to draw their table cell, it always reuses it so that it doesn’t have allocate and deallocate it over and over again. Whenever a cell appears, UITableView and UICollectionView reuse a Class that draws the cell.

Furthermore, that cell class uses drawRect method to specify how it is drawn.

Add method interface for RSDFDatePickerViewDataSource

This is where we add our custom shouldColorDate method for the delegate. That way, we are saying that external UIViewControllers would have to implement this method if they conform to our RSDFDatePickerViewDataSource protocol.

Implementing this method involves letting us know which dates to color. This date is passed in through the parameter as shown below.

Using your RSDFDatePickerView

Add the property to the class extension in the .m file. We will be doing a custom access method and NOT use synthesize. Make sure you conform to RSDFDatePickerView and its source delegates. That way, you can implement those methods below and let RSDFDatePickerView which dates to color.

Write your own Access method

Add the datePickerView to your views

Implement the method on the RSDFDatePickerViewDataSource method

Once you conformed to the protocol, you can now implement the protocol method. Here, we implement shouldColorDate which gives you the date. This means that you need to let it know whether this date is to be colored or not. We do this by checking to see if this date exists inside an array called datesToMark. If it does, let’s color it by returning TRUE. Otherwise, don’t color it by returning false.

Custom Features

Let’s say now you want to add your custom drawing to the RSDF Cells. In the project, you are outfitted with RSDFCustomDatePickerView files.

In RSDFCustomDatePickerView.m, we need to make sure MyDayCell class name returns the class of the RSDFCustomDatePickerDaycell class because that’s the class that has all the over-ridden methods to return the custom attributes of how we want to draw the cell.

On a side note, when we create our collection view, we need to register our cell class to be reused with a identifier. We have reigstered two:

  • RSDFDatePickerViewDayCellIdentifier for [self dayCellClass]
  • MyDayCellIdentifier for [self myDayCellClass]

Also, shown below:

So we registered our custom day cell of [self myDayCellClass] which returns [RSDFCustomDatePickerDayCell class];

The reason why is because when our custom MyDayCell class is being used in method cellForItemAtIndexPath in RSDFDatePickerView.m, it gets returned like this:

And as we all remember, in RSDFCustomDatePickerDayCell.h, the RSDFCustomDatePickerDayCell class derives from MyDayCell so its a valid assignment:

Hence, in our custom RSDF picker view (RSDFCustomDatePickerView.m) we must do this:

In RSFDatePickerDayCell, we must make sure our custom cell class derives from MyDayCell instead of the original RSDFDatePickerDayCell. In turn, in our MyDayCell, it is derived from RSDFDatePickerDayCell.