zoukankan      html  css  js  c++  java
  • 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);                        //添加
            }
    
        }
    }

  • 相关阅读:
    范仁义js课程---59、this
    javascript疑难问题---9、闭包执行问题
    jetbrains
    React-Native首次运行提示-ReferenceError-Can-t-find-variable-fbBatchedBridge
    visual studio 2015提示IE10未安装
    【转】在Windows下搭建React Native Android开发环境
    Android Studio 简单设置
    【转】Spring 4.x实现Restful web service
    SecureCRT 终端仿真程序 v7.0.0.326 中文绿色便携破解版
    《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
  • 原文地址:https://www.cnblogs.com/IKang8701348/p/6254146.html
Copyright © 2011-2022 走看看