Hello
I'm making sticky header in my UICollectionViewController (class UltimaCollectionViewController : UICollectionViewController) also this class provides vertical and horizontal scrolling In iPad simulator everything works fine. But on two different real iPad the method GetViewForSupplementaryElement doesn't get called.
By the way, public override void ViewWillDisappear (bool animated) do get called only on simulator.
Do anybody have some ideas why it is so?
Relevant code parts:
public partial class UltimaCollectionViewController : UICollectionViewController { static NSString headerId = new NSString ("Header");
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
CollectionView.RegisterClassForCell (typeof(GridCell), GridCellCellId);
CollectionView.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, headerId);
GenereteDetalisation ();
AddExportExcelButton ();
// Can have a bug - device not sleep
UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications ();
NSNotificationCenter.DefaultCenter.AddObserver ("UIDeviceOrientationDidChangeNotification", ChangeNotify);
}
public override void ViewWillDisappear (bool animated)
{
//bug - does not invoked
this.NavigationController.ToolbarHidden = true;
base.ViewWillDisappear (animated);
}
public override void ViewWillAppear (bool animated) { CreateGridCollection ();
CollectionView.Delegate = new CustomFlowLayoutDelegate (gridCollection);
CollectionView.CollectionViewLayout = new UltimaCollectionViewLayout (gridCollection, spaceForHeader);
CollectionView.Pinch().Pinch -= HandlePinch;
CollectionView.Pinch().Pinch += HandlePinch;
UIView view = new UIView(new RectangleF(0, 0, this.View.Frame.Width, this.View.Frame.Height));
view.BackgroundColor = UIColor.GroupTableViewBackgroundColor;
this.CollectionView.BackgroundView = view;
base.ViewWillAppear (animated);
}
public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
var gridCell = (GridCell)collectionView.DequeueReusableCell (GridCellCellId, indexPath);
var cell = gridCollection.GetCell (indexPath.Row);
var key = gridCollection.GetRowColIndex (indexPath.Row);
var column = gridCollection.GetColumn (key.Value);
gridCell.UpdateCell(column, cell);
//PointF loc = new PointF (headerView.Frame.X, this.CollectionView.ContentOffset.Y);
//headerView.Frame = new RectangleF (loc, headerView.Frame.Size);
return gridCell;
}
public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var hw = (Header)collectionView.DequeueReusableSupplementaryView (elementKind, headerId, indexPath);
hw.UpdateHeader (gridCollection.Columns(), spaceForHeader);
return hw;
}
and
public class UltimaCollectionViewLayout : UICollectionViewFlowLayout { private ReportGridCollection grid; SizeF collectionViewContentSize; /// /// The space between columns and between rows /// -1 for same border width inside and outside table /// float space = -1f;
UICollectionViewLayoutAttributes header;
public UltimaCollectionViewLayout (ReportGridCollection grid, float space)
{
this.HeaderReferenceSize = new SizeF (1000, 22);
}
public override bool ShouldInvalidateLayoutForBoundsChange (RectangleF newBounds)
{
return true;
}
public override UICollectionViewLayoutAttributes LayoutAttributesForItem (NSIndexPath indexPath)
{
//return base.LayoutAttributesForItem (indexPath);
//if indexPath.
var a = UICollectionViewLayoutAttributes.CreateForCell (indexPath);
var key = grid.GetRowColIndex (indexPath.Row);
var column = grid.GetColumn (key.Value);
float w = column.Width;
float h = column.Height;
a.Size = new SizeF (w, h);
float x = -w / 2;
for (int i = 0; i <= key.Value; i++) {
x += grid.GetColumn (i).Width + space;
}
float y = h / 2 + key.Key * (h + space);
a.Center = new PointF (x, y);
return a;
}
public UICollectionViewLayoutAttributes LayoutAttributesForHeaderView ()
{
NSIndexPath ip = NSIndexPath.FromItemSection (0, 0);
var la = UICollectionViewLayoutAttributes.CreateForSupplementaryView (UICollectionElementKindSection.Header, ip);
UICollectionView cv = this.CollectionView;
PointF contentOffset = cv.ContentOffset;
int s = 0;
int numberOfItemsInSection = cv.NumberOfItemsInSection (s);
NSIndexPath fcip = NSIndexPath.FromItemSection (0, s);
NSIndexPath lcip = NSIndexPath.FromItemSection (Math.Max(0, numberOfItemsInSection-1), s);
var fcla = this.LayoutAttributesForItem (fcip);
var lcla = this.LayoutAttributesForItem (lcip);
float height = grid.Columns()[0].Height;
float width = grid.Columns ().Sum (w => w.Width + space);
PointF location = la.Frame.Location;
location.Y = Math.Min(Math.Max(contentOffset.Y, fcla.Frame.Y - height),
lcla.Frame.Y + lcla.Frame.Height - height
);
la.ZIndex = 1024;
la.Frame = new RectangleF (location, new SizeF(width, height));
return la;
}
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect (RectangleF rect)
{
float h = grid.GetColumn (0).Height;
int minRow = rect.Location.Y > 0 ? (int)(rect.Location.Y / (h + space)) : 0;
int maxRow = (int)(rect.Size.Height / (h + space) + minRow);
maxRow = Math.Min (maxRow, grid.RowCount);
var answer = new List<UICollectionViewLayoutAttributes> ();
for (int j = minRow; j < maxRow; j++) {
for (int i = 0; i < grid.ColumnsCount; i++) {
int ind = j * grid.ColumnsCount + i;
NSIndexPath ip = NSIndexPath.FromItemSection (ind, 0);
answer.Add (LayoutAttributesForItem (ip));
}
}
answer.Add (LayoutAttributesForHeaderView ());
return answer.ToArray ();
}
}
and header class
public class Header : UICollectionReusableView { UIView header;
[Export ("initWithFrame:")]
public Header (RectangleF frame) : base (frame)
{
header = new UIView ();
AddSubview (header);
}
public void UpdateHeader(IList<ReportColumn> reportColumns, float spaceForHeader){
float prevTotalWidth = 0;
foreach (var item in reportColumns) {
var w = item.Width;
var h = item.Height;
UILabel label = new UILabel (new RectangleF (prevTotalWidth, 0, w, h));
label.Text = item.Caption;
label.Tag = item.ID;
label.Font = UIFont.SystemFontOfSize (15);
label.TextAlignment = GridCell.ConvertToUITextAlignment (TextAlignment.Center);
//label.BackgroundColor = UIColor.LightTextColor;
label.BackgroundColor = UIColor.White;
header.AddSubview (label);
prevTotalWidth += w + spaceForHeader;
}
header.Frame = new RectangleF (0, 0, prevTotalWidth, reportColumns[0].Height);
}
}