如何实现 UICollectionView - XAMARIN.IOS [英] How to implement UICollectionView - XAMARIN.IOS

查看:32
本文介绍了如何实现 UICollectionView - XAMARIN.IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UICollectionView,但找不到任何可以利用的示例.我需要 UICollectionView 代码(不使用 swift/storyboard/forms).你能给我一个非常简单的例子吗?例如2行2列好吗?基本的东西只是为了尝试了解我如何实现它.

谢谢

解决方案

可以参考

这是示例链接供参考.

I'm trying to use UICollectionView but I can't find any samples that I can take advantage of. I needed the UICollectionView by code (without using swift/storyboard/forms). Could you give me a really simple example? For example 2 lines with 2 columns please? Basic stuff just to try to understand how I can implement it.

Thank you

解决方案

You can refer to Collection Views in Xamarin.iOS doc to check how to use Collection View with Code .And here I will show a sample code to explain how to implement it .

Could you give me a really simple example? For example 2 lines with 2 columns please?

First , need to create a GridLayout :

public class GridLayout : UICollectionViewFlowLayout
{
    public GridLayout ()
    {
    }

    public override bool ShouldInvalidateLayoutForBoundsChange (CGRect newBounds)
    {
        return true;
    }

    public override UICollectionViewLayoutAttributes LayoutAttributesForItem (NSIndexPath path)
    {
        return base.LayoutAttributesForItem (path);
    }

    public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect (CGRect rect)
    {
        return base.LayoutAttributesForElementsInRect (rect);
    }
}

Then you can init the Collection View in ViewDidLoad :

static NSString animalCellId = new NSString("AnimalCell");
List<IAnimal> animals;

animals = new List<IAnimal>();
for (int i = 0; i < 2; i++)
{
    animals.Add(new Monkey());
}

// Perform any additional setup after loading the view, typically from a nib.
UICollectionView collectionView = new UICollectionView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 300), new GridLayout());
collectionView.RegisterClassForCell(typeof(AnimalCell), animalCellId);
collectionView.BackgroundColor = UIColor.Blue;
collectionView.DataSource = new MyCollectionViewDataDelegate(animals);
View.AddSubview(collectionView);

Here you also need to creat a Custom Cell for your needs , this can be modified by yourself :

public class AnimalCell : UICollectionViewCell
{
    UIImageView imageView;

    [Export("initWithFrame:")]
    public AnimalCell(CGRect frame) : base(frame)
    {
        BackgroundView = new UIView { BackgroundColor = UIColor.Orange };

        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Green };

        ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
        ContentView.Layer.BorderWidth = 2.0f;
        ContentView.BackgroundColor = UIColor.White;
        ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);

        imageView = new UIImageView(UIImage.FromBundle("placeholder.png"));
        imageView.Center = ContentView.Center;
        imageView.Transform = CGAffineTransform.MakeScale(0.7f, 0.7f);

        ContentView.AddSubview(imageView);
    }

    public UIImage Image
    {
        set
        {
            imageView.Image = value;
        }
    }

    [Export("custom")]
    void Custom()
    {
        // Put all your custom menu behavior code here
        Console.WriteLine("custom in the cell");
    }


    public override bool CanPerform(Selector action, NSObject withSender)
    {
        if (action == new Selector("custom"))
            return true;
        else
            return false;
    }
}

The MyCollectionViewDataDelegate also need to be created:

public class MyCollectionViewDataDelegate : UICollectionViewDataSource { private List animals;

    public MyCollectionViewDataDelegate(List<IAnimal> animals)
    {
        this.animals = animals;
    }

    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 2;
    }

    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return animals.Count;
    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var animalCell = (AnimalCell)collectionView.DequeueReusableCell(animalCellId, indexPath);

        var animal = animals[indexPath.Row];

        animalCell.Image = animal.Image;

        return animalCell;
    }

}

You can find that animalCell should be registed when init Collection View .

Then effect :

This is the sample link for reference .

这篇关于如何实现 UICollectionView - XAMARIN.IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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