UICollectionViewCell 网格显示数据

using System;
using System.Collections.Generic;
using Foundation;
using UIKit;

namespace ddd
{
    public partial class ViewController : UIViewController
    {
        private List<UIImage> collectionData;
        private UICollectionView collectionView;
        private UICollectionViewFlowLayout collectionViewLayout; 
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        } 
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            uiConfig();


        }
        private void uiConfig()
        {
            //设置假的数据源数组  
            collectionData = new List<UIImage>();
            for (int i = 0; i < 50; i++)
            {
                collectionData.Add(UIImage.FromFile("1.jpg"));
            }
            //初始化UICollectionView  
            collectionViewLayout = new UICollectionViewFlowLayout();
            collectionViewLayout.MinimumLineSpacing = 20f;//设置行距  
            collectionViewLayout.MinimumInteritemSpacing = 10f;//设置列距  
            collectionViewLayout.SectionInset = new UIEdgeInsets(20, 20, 20, 20);//设置边界  
            collectionViewLayout.ItemSize = new CoreGraphics.CGSize(70f, 70f);//设置网格大小  

            collectionView = new UICollectionView(this.View.Bounds, collectionViewLayout);
            collectionView.BackgroundColor = UIColor.LightTextColor;
            collectionView.RegisterNibForCell(ImageCell.Nib, "ImageCell");
            collectionView.Source = new collectionSource(this);
            this.View.AddSubview(collectionView);
        }
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        } 

        private class collectionSource : UICollectionViewSource
        {
            ViewController owner;

            public collectionSource(ViewController owner)
            {
                this.owner = owner;
            }
            //设置网格个数
            public override nint GetItemsCount(UICollectionView collectionView, nint section)
            {
                return owner.collectionData.Count;
            }
            //设置网格中的内容
            public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
            {
                ImageCell cell = (ImageCell)collectionView.DequeueReusableCell((NSString)ImageCell.Key, indexPath);
                cell.ImageView.Image = owner.collectionData[indexPath.Row];
                return cell;
            }
            //选中某一个网格
            public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
            {
                UIAlertView alert = new UIAlertView();
                alert.Title = string.Format("你选择了第{0}个网格", indexPath.Row);
                alert.AddButton("确定");
                alert.Show();
            }

        }
    }

    public partial class ImageCell : UICollectionViewCell
    {
        public static readonly NSString Key = new NSString("ImageCell");
        public static readonly UINib Nib;

        static ImageCell()
        {
            Nib = UINib.FromName("ImageCell", NSBundle.MainBundle);
        }

        protected ImageCell(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
            this.Initialize();              //调用初始化方法
        }
        //设置属性
        public UIImageView ImageView
        {
            get;
            private set;
        }
        //初始化
        private void Initialize()
        {
            this.ImageView = new UIImageView(this.ContentView.Bounds);          //实例化对象
            this.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;      //设置显示模式
            this.ContentView.AddSubview(this.ImageView);                        //添加
        }

    }
}

 

posted @ 2017-01-05 21:38  iZKang  阅读(428)  评论(0编辑  收藏  举报