Joseph Joseph - 3 months ago 18
iOS Question

GetViewForSupplementaryElement not getting called when trying to show heading in UICollectionView

I am trying to setup a

UICollectionView
within my existing
UIViewController
. Everything is working except for getting a title to show for each section - I can't figure out what I'm doing wrong.

My code in the
UIViewController
to initiate the collection view:

public partial class ViewController : UIViewController
{
//...

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

CollectionView_Outlet.RegisterClassForCell(typeof(ModifierCell), ModifierCell.CellID);
CollectionView_Outlet.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, Header.HeaderId);
CollectionView_Outlet.ShowsHorizontalScrollIndicator = false;
CollectionView_Outlet.Source = new ModifiersSource(this);
CollectionView_Outlet.BackgroundColor = UIColor.White;
CollectionView_Outlet.ReloadData();
}

//...
}


Then I have created a subclass of
UICollectionViewSource
:

public class ModifiersSource : UICollectionViewSource
{
ViewController senderVC;

public ModifiersSource(ViewController sender)
{
senderVC = sender;
}

public override nint NumberOfSections(UICollectionView collectionView)
{
return 2;
}

public override nint GetItemsCount (UICollectionView collectionView, nint section)
{
return senderVC.modifiers.Count;
}

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
//...
}


public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (Header)collectionView.DequeueReusableSupplementaryView (elementKind, Header.HeaderId, indexPath);
headerView.Text = "Supplementary View";
return headerView;
}

}


And finally created:

public class Header : UICollectionReusableView
{
public static NSString HeaderId = new NSString("UserSource1");
UILabel label;

public string Text {
get {
return label.Text;
}
set {
label.Text = value;
SetNeedsDisplay ();
}
}

[Export ("initWithFrame:")]
public Header (RectangleF frame) : base (frame)
{
label = new UILabel (){
Frame = new RectangleF(0,0,300,50),
BackgroundColor = UIColor.Red};
AddSubview (label);
BackgroundColor = UIColor.White;
}
}


I've put a breakpoint on the
GetViewForSupplementaryElement
method but it never gets called. I've also set the following in my StoryBoard:

XCode Screenshot

What am I missing?!

Answer

After many attempts of not being able to get the above to work, I manually set UICollectionViewFlowLayout whilst initiating the UIContainerView. Seems to have done the trick, but not sure why it didn't pick up the settings from my StoryBoard. Here is my working code:

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        CollectionView_Outlet.RegisterClassForCell(typeof(ModifierCell), ModifierCell.CellID);
        CollectionView_Outlet.RegisterClassForCell(typeof(ItemOptionCell), ItemOptionCell.CellID);
        CollectionView_Outlet.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, Header.HeaderId);
        CollectionView_Outlet.ShowsHorizontalScrollIndicator = false;

        //This is the new bit I added:
        var layout = new UICollectionViewFlowLayout ();
        layout.HeaderReferenceSize = new CGSize (300, 40);
        CollectionView_Outlet.SetCollectionViewLayout (layout, false);

        CollectionView_Outlet.Source = new ModifiersSource(this);
        CollectionView_Outlet.ReloadData();
    }