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 = ; i < ; i++)
{
collectionData.Add(UIImage.FromFile("1.jpg"));
}
//初始化UICollectionView
collectionViewLayout = new UICollectionViewFlowLayout();
collectionViewLayout.MinimumLineSpacing = 20f;//设置行距
collectionViewLayout.MinimumInteritemSpacing = 10f;//设置列距
collectionViewLayout.SectionInset = new UIEdgeInsets(, , , );//设置边界
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); //添加
} }
}

最新文章

  1. 利用PHP的ob函数实现生成静态化页面
  2. POJ2406Power Strings[KMP 失配函数]
  3. hdu 2509 博弈 *
  4. chrome45以后的版本安装lodop后,仍提示未安装解决
  5. hdu 2047 阿牛的EOF牛肉串
  6. img base64
  7. 选择Android还是选择JavaEE?
  8. Android 建造者(Builder)模式
  9. [ACM] hdu 4405 Aeroplane chess (概率DP)
  10. 将百度坐标转换的javascript api官方示例改写成传统的回调函数形式
  11. 霍尔开关MH253ESO在减压神器指尖手指陀螺中的作用
  12. 理论篇:关注点分离(Separation of concerns, SoC)
  13. 破解附近寝室的Wifi密码
  14. Github项目推荐-图神经网络(GNN)相关资源大列表
  15. Delphi 数据转换
  16. scrapy流程
  17. 前端vue框架 路由的安装及使用
  18. Jmeter脚本录制方法(二)手工编写脚本(jmeter与fiddler结合使用)
  19. Nginx浏览目录配置及美化
  20. vue中定义多重样式

热门文章

  1. CSS3之3d变换与关键帧
  2. Atitit ati&#160;licenseService &#160;&#160;&#160;设计原理
  3. iOS多线程之8.NSOPeration的其他用法
  4. android Canvas 和 Paint用法
  5. iOS开发常用快捷键
  6. pip安装指定版本的package
  7. 【译】Spring 4 Hello World例子
  8. overflow:hidden与margin:0 auto之间的冲突
  9. mysql数据库主从及主主复制配置演示
  10. Handler(二)