Я пытаюсь установить UICollectionView
в рамках существующего UIViewController
. Все работает, за исключением получения названия для каждого раздела - я не могу понять, что я делаю неправильно.GetViewForSupplementaryElement не получает вызов при попытке показать заголовок в UICollectionView
Мой код в UIViewController
инициировать представление коллекции:
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();
}
//...
}
Затем я создал подкласс 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;
}
}
И, наконец, создано:
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 Положим точку останова на метод GetViewForSupplementaryElement
, но он никогда не вызывается. Я также установил следующее в моем раскадровке:
Что мне не хватает ?!
Это не работает для меня. Я создаю коллекцию в программном виде и устанавливаю headerreferenceisize, а также используя UICollectionViewDelegateFlowLayout и переопределяя GetReferenceSizeForHeader. Даже выполнение этого метода GetViewForSupplementaryElement не вызывается. Все-таки это ошибка в ксамарине? – rbmanian75