xamarin.ios - 如何为 Mv uiCollectionViewController 提供页眉或页脚?

标签 xamarin.ios uicollectionview xamarin mvvmcross

我试图从 MvxCollectionViewController 中提供页眉和页脚 View ,但我遇到了麻烦。通常,对于 UICollectionViewController,我会像这样覆盖 GetViewForSupplementaryElement 方法:

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

MvxCollectionViewController 似乎没有像 UICollectionViewController 那样获得对 GetViewForSupplementaryElement 方法的委托(delegate)回调。

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

最佳答案

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

  1. 为布局中的页眉提供大小

        HeaderReferenceSize = new System.Drawing.SizeF(100, 100),
    
  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);
        }
    }
    
  3. 注册该类(class)

       CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, headerId); 
    
  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;
    }
    

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


Aside> 有一个 Unresolved 问题来提供可绑定(bind)的补充意见 - https://github.com/slodge/MvvmCross/issues/339 - 但此问题不应影响基本的 Collection View 操作。

关于xamarin.ios - 如何为 Mv uiCollectionViewController 提供页眉或页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18861700/

相关文章:

objective-c - 在 Cocoa 中将一个 NSView 替换为另一个 NSView

android - Xamarin Android 简单输入文本弹出窗口

ios - 如何实例化 ARSCNView

ios - 以编程方式在 UIScrollView 中添加一些 UITextView 和其他元素 Xamarin IOS

ios - 从项目中的文件夹加载图像

ios - 快速 fatal error 中的自定义 CollectionViewCell

c# - 使用 Xamarin 从 iPhone 获取数据到 Apple Watch

iphone - 将 C# 编译成 objective-C

xamarin - 向 Xamarin Forms iOS 中的条目添加底部边框,最后是图像

ios - UICollectionView 水平滚动