如何为MvxCollectionViewController提供页眉或页脚?

dan*_*anb 2 xamarin.ios mvvmcross xamarin uicollectionview

我试图从MvxCollectionViewController中提供页眉和页脚视图,我遇到了麻烦.通常,使用UICollectionViewController,我会覆盖GetViewForSupplementaryElement方法,如下所示:

public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
    var someHeaderOrFooterView = (HeaderOrFooterView) collectionView.DequeueReusableSupplementaryView (elementKind, elementId, indexPath);
    return someHeaderOrFooterView;
}
Run Code Online (Sandbox Code Playgroud)

MvxCollectionViewControllers似乎没有像UICollectionViewController那样获得GetViewForSupplementaryElement方法的委托回调.

是否有另一种方法使用MvxCollectionViewController指定CollectionView的页眉和页脚?

Stu*_*art 6

标准步骤应该有效(它们在这里工作......):

  1. 为布局中的标题提供大小

        HeaderReferenceSize = new System.Drawing.SizeF(100, 100),
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为标头实现一个类

    public class Header : UICollectionReusableView
    {
        UILabel label;
    
        public string Text
        {
            get { return label.Text; }
            set { label.Text = value; SetNeedsDisplay(); }
        }
    
        [Export("initWithFrame:")]
        public Header(System.Drawing.RectangleF frame)
            : base(frame)
        {
            label = new UILabel() { Frame = new System.Drawing.RectangleF(0, 0, 300, 50), BackgroundColor = UIColor.Yellow };
            AddSubview(label);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 注册那个班级

       CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, headerId); 
    
    Run Code Online (Sandbox Code Playgroud)
  4. 实现GetViewForSupplementaryElement

    public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
    {
        var headerView = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, headerId, indexPath);
        headerView.Text = "Supplementary View";
        return headerView;
    }
    
    Run Code Online (Sandbox Code Playgroud)

刚刚在这里测试了这些步骤,它们可以在我的示例应用程序中运行(基于https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-11-KittenView_Collections).


Aside>提供可绑定的补充视图有一个未解决的问题 - https://github.com/slodge/MvvmCross/issues/339 - 但是这个问题不应该影响基本的集合视图操作.