子类化 UICollectionView [英] Subclassing UICollectionView

查看:30
本文介绍了子类化 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在 Xamarin.iOS 中对 UICollectionView 进行子类化吗?

Does anybody know how to subclass a UICollectionView in Xamarin.iOS?

我试过了

public class MyCustomUICollectionView : UICollectionView
{

    [Export ("initWithFrame:")]
    public InfinitiveScrollingUICollectionView (CGRect frame) : base(frame)
    {
        // initialization
    }
}

但我明白

UIKit.UICollectionView.UICollection(Foundation.NSCoder) 的最佳重载方法匹配有一些无效参数参数 #1 无法将CoreGraphics.CGRect表达式转换为类型Foundation.NSCoder`.

The best overloaded method match for UIKit.UICollectionView.UICollection(Foundation.NSCoder) has some invalid arguments Argument #1 cannot convertCoreGraphics.CGRectexpression to typeFoundation.NSCoder`.

我也尝试使用 public InfinitiveScrollingUICollectionView () 但我得到了

I also tried to use public InfinitiveScrollingUICollectionView () but I get

类型 UIKit.UICollectionView 不包含接受0"参数的构造函数

The type UIKit.UICollectionView does not contain a constructor that takes '0' arguments

我想覆盖 LayoutSubviews.还是应该将 UICollectionViewController 用于这样的目的?

I want to override LayoutSubviews. Or should the UICollectionViewController be used for such a purpose?

推荐答案

UICollectionView 没有接受单个 CGRect 的构造函数,所以你也必须传递一个布局:

UICollectionView has no constructor that accepts a single CGRect, so you have to pass a layout, too:

public class MyCustomUICollectionView : UICollectionView
{
    public MyCustomUICollectionView(CGRect frame, UICollectionViewLayout layout) 
        : base(frame, layout)
    {
    }
}

如果你愿意,你也可以在内部创建布局,这样你就不必从外部传递:

If you want to, you can also create the layout internally, so you don't have to pass it from the outside:

public class MyCustomUICollectionView : UICollectionView
{
    private static readonly UICollectionViewLayout _layout;

    static MyCustomUICollectionView()
    {
        // Just an example
        _layout = new UICollectionViewFlowLayout();
    }

    public MyCustomUICollectionView(CGRect frame) : base(frame, _layout)
    {
    }
}

这篇关于子类化 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆