How to implement UICollectionView - XAMARIN.IOS

佐手、 提交于 2021-01-29 13:32:04

问题


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


回答1:


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 .



来源:https://stackoverflow.com/questions/59534202/how-to-implement-uicollectionview-xamarin-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!