我试图从 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 的页眉和页脚?
最佳答案
标准步骤应该有效(它们在这里有效...):
为布局中的页眉提供大小
HeaderReferenceSize = new System.Drawing.SizeF(100, 100),
为标题实现一个类
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); } }
注册该类(class)
CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, headerId);
实现 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/