Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Swift中UICollectionView的简单用法

之前写过OC中collectionView的用法,现在再看看swift中collectionView的用法,有兴趣的朋友,可以两者前后比较下区别,swift现在没有稳定下来,语法更新的比较快,但是它核心的一些东西,已经定型了。这些还是靠读者们自己去挖掘吧。总得来说,swift无论是在语法上,还是在其书写内容上,和OC有着千丝万缕的联系,但又不完全一样,有的地方,进行了优化。当然这些都是笔者个人理解,毕竟也是从头开始研究这个语言,可能有些不对的地方,也希望各位能够指出。//签署数据源和代理,此时不需要引入layout的代理。//此属性设置为false可以解决由于scrollview引起的界面bugself.automaticallyAdjustsScrollViewInsets = false;代码我就全部上来了,一个小demo,没多少代码,里面的注释写的还算详细//这里签署数据源和代理,此时不需要引入layout的代理,也可以。   class AmonViewController: UIViewController       ,UICollectionViewDataSource,UICollectionViewDelegate{  var _collectionView:UICollectionView?;  var _dataArray:NSArray?;  override func viewWillAppear(animated: Bool) {  super.viewWillAppear(animated);  self.tabBarController?.tabBar.hidden = false;  }  override func viewDidLoad() {    super.viewDidLoad()  self.title = "排行";  //此属性设置为false可以解决由于scrollview引起的界面bug  self.automaticallyAdjustsScrollViewInsets = false;  self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor();  let headerView = AmonCirHeadView(frame: CGRectMake(0, 64, kScreenW, 150));  self.view.addSubview(headerView);  //创建layout   let layOut = UICollectionViewFlowLayout();  //设置滚动方向  //        layOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;  layOut.scrollDirection = UICollectionViewScrollDirection.Horizontal;  layOut.itemSize = CGSizeMake(kScreenW, kScreenH-headerView.bounds.size.height-49-   64);  //垂直方向间距   layOut.minimumInteritemSpacing = 0;  //水平方向间距  layOut.minimumLineSpacing = 0;  //设置四周得边缘距离  layOut.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);  _collectionView = UICollectionView(frame: CGRectMake(0, headerView.bottom, kScreenW, kScreenH-headerView.bounds.size.height-49-64), collectionViewLayout: layOut);  self.view.addSubview(_collectionView!);  _collectionView?.delegate = self;    _collectionView?.dataSource = self;//是否分页  _collectionView?.pagingEnabled = true;  _collectionView?.backgroundColor = UIColor.whiteColor();  _collectionView?.registerClass(UICollectionViewCell().classForCoder,   forCellWithReuseIdentifier: "item");  }//data Sourcefunc numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {return 1;}func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {return 10;}func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {let cell = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath);var imgV = cell.contentView.viewWithTag(2016052519) as? UIImageView;var lab = cell.contentView.viewWithTag(2016052520) as? UILabel;if imgV == nil{imgV = UIImageView(frame: cell.bounds);cell.contentView.addSubview(imgV!);imgV?.tag = 2016052519;imgV!.image = UIImage(named: "AION-天气背景.jpg");lab = UILabel();lab?.frame = CGRectMake(0, 0, kScreenW, 30);lab!.tag = 2016052520;lab?.textAlignment = NSTextAlignment.Center;lab?.textColor = UIColor.whiteColor();lab?.backgroundColor = UIColor.blackColor();lab?.alpha = 0.3;cell.contentView.addSubview(lab!);lab?.numberOfLines = 0;lab?.font = UIFont.systemFontOfSize(12);lab?.text = "最好看的小说,尽在BQPReader";}return cell;}//delegatefunc collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {}}下面关于Swift的内容你可能也喜欢:Ubuntu 14.04.4 下安装 Swift 2.2.1  http://www.linuxidc.com/Linux/2016-05/131249.htmUbuntu 15.10安装部署Swift开发环境  http://www.linuxidc.com/Linux/2016-01/126995.htm Swift 的变化:从 2.2 到 3.0 会带来什么  http://www.linuxidc.com/Linux/2015-12/126440.htm Swift 正式开源,同时开源 Swfit 核心库和包管理器 http://www.linuxidc.com/Linux/2015-12/125847.htm Apple Swift学习教程  http://www.linuxidc.com/Linux/2014-09/106420.htm 使用 Swift 构建一个 iOS 的邮件应用 http://www.linuxidc.com/Linux/2014-08/105542.htm Swift 2.0开源化  http://www.linuxidc.com/Linux/2015-06/118594.htm Linux下搭建Swift语言开发学习环境 http://www.linuxidc.com/Linux/2015-12/125983.htm Swift 的详细介绍:请点这里 本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132107.htm