Swift纯代码走进UICollectionView

Swift纯代码走进UICollectionView,第1张

概述Swift对于一门新的iOS编程语言,他的崛起是必然的 我们这群老程序员们学习新的技能也是必然的 不接受新技能将被这大群体无情的淘汰 So 我欣然接受这门看似不成熟的语言 下面我们说说Swift中比较常见的控件UICollectionView 首先我们设置一个全局的UICollectionView和一个数据源 var colltionView : UICollectionView? var dat

Swift对于一门新的iOS编程语言,他的崛起是必然的

我们这群老程序员们学习新的技能也是必然的

不接受新技能将被这大群体无情的淘汰

So 我欣然接受这门看似不成熟的语言

下面我们说说Swift中比较常见的控件UICollectionVIEw

首先我们设置一个全局的UICollectionVIEw和一个数据源

var colltionVIEw : UICollectionVIEw?
var dataArr = NSMutableArray()

然后设置UICollectionVIEw的3个代理
UICollectionVIEwDelegate,UICollectionVIEwDataSource,UICollectionVIEwDelegateFlowLayout
接下来我们要做的是overrIDe func vIEwDIDLoad()方法中初始化一些必要的对象

overrIDe func vIEwDIDLoad() {  super.vIEwDIDLoad()  var layout = UICollectionVIEwFlowLayout()  colltionVIEw = UICollectionVIEw(frame: CGRectMake(0,0,wIDth,height),collectionVIEwLayout: layout)  //注册一个cell  colltionVIEw! .registerClass(Home_Cell.self,forCellWithReuseIDentifIEr:"cell")  //注册一个headVIEw  colltionVIEw! .registerClass(Home_headVIEw.self,forSupplementaryVIEwOfKind:UICollectionElementKindSectionheader,withReuseIDentifIEr: "headVIEw")  colltionVIEw?.delegate = self;  colltionVIEw?.dataSource = self;  colltionVIEw?.backgroundcolor = UIcolor.whitecolor()  //设置每一个cell的宽高  layout.itemSize = CGSizeMake((wIDth-30)/2,250)  self.vIEw .addSubvIEw(colltionVIEw!)  self .getData()}

然后我们实现UICollectionVIEw的代理方法

//返回多少个组func numberOfSectionsInCollectionVIEw(collectionVIEw: UICollectionVIEw) -> Int {  return 1}//返回多少个cellfunc collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {  return dataArr.count}//返回自定义的cellfunc collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell {  let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr("cell",forIndexPath: indexPath) as! Home_Cell  var model = GoodsModel()  model = dataArr[indexPath.row] as! GoodsModel  let url : NSURL = NSURL(string: model.image_url as String)!  cell.imgVIEw!.hnk_setimageFromURL(url)  cell.layer.borderWIDth = 0.3;  cell.layer.bordercolor = UIcolor.lightGraycolor().CGcolor  cell.TitleLabel!.text = model.short_name  cell.priceLabel!.text = "¥"+model.p_price  cell.readLabel!.text = "��"+model.like_count  return cell}//返回headVIEw的宽高func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,referenceSizeforheaderInSection section: Int) -> CGSize{  return CGSize(wIDth: wIDth,height: height/1.6)}//返回自定义headVIEw或者FootVIEw,我这里以headvIEw为例func collectionVIEw(collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,atIndexPath indexPath: NSIndexPath) -> UICollectionReusableVIEw{  var v = Home_headVIEw()  if kind == UICollectionElementKindSectionheader{  v = colltionVIEw!.dequeueReusableSupplementaryVIEwOfKind(kind,withReuseIDentifIEr: "headVIEw",forIndexPath: indexPath) as! Home_headVIEw  }  return v}//返回cell 上下左右的间距func collectionVIEw(collectionVIEw: UICollectionVIEw,insetForSectionAtIndex section: Int) -> UIEdgeInsets{  return UIEdgeInsetsMake(5,10,5,10)}

然后我们来获取数据,这里的话我用的是Alamofire进行的网络请求,URL不方便透露

//获取数据func getData(){  Alamofire.request(.GET,GoodsUrl).responseJsON() { (req,_,JsON,_) -> VoID in    if let j = JsON as? NSDictionary{      var data = j.valueForKey("data")as! NSArray      for dict in data{        var model = GoodsModel()        model.Analytical(dict as! NSDictionary)        self.dataArr.addobject(model)      }      self.colltionVIEw!.reloadData()    }  }}

接下来让我们看下cell里面究竟写了些什么玩意

class Home_Cell: UICollectionVIEwCell {  let wIDth = UIScreen.mainScreen().bounds.size.wIDth//获取屏幕宽  var imgVIEw : UIImageVIEw?//cell上的图片  var TitleLabel:UILabel?//cell上Title  var priceLabel:UILabel?//cell上价格  var readLabel:UILabel?//cell上的阅读量overrIDe init(frame: CGRect) {  super.init(frame: frame)  //初始化各种控件  imgVIEw = UIImageVIEw(frame: CGRectMake(0,-10,(wIDth-30)/2,200))  self .addSubvIEw(imgVIEw!)  TitleLabel = UILabel(frame: CGRectMake(5,CGRectGetMaxY(imgVIEw!.frame)-12,(wIDth-40)/2,50))  TitleLabel?.numberOflines = 0  TitleLabel?.Font = UIFont.boldSystemFontOfSize(14.0)  TitleLabel?.textcolor = UIcolor.lightGraycolor()  self .addSubvIEw(TitleLabel!)  priceLabel = UILabel(frame: CGRectMake(5,CGRectGetMaxY(TitleLabel!.frame),(wIDth-40)/2/2,20))  priceLabel?.numberOflines = 0  priceLabel?.Font = UIFont.boldSystemFontOfSize(14.0)  priceLabel?.textcolor = UIcolor.lightGraycolor()  self .addSubvIEw(priceLabel!)  readLabel = UILabel(frame: CGRectMake((wIDth-30)/2/2,20))  readLabel?.numberOflines = 0  readLabel?.textAlignment = NSTextAlignment.Right  readLabel?.Font = UIFont.boldSystemFontOfSize(14.0)  readLabel?.textcolor = UIcolor.lightGraycolor()  self .addSubvIEw(readLabel!)}required init(coder aDecoder: NSCoder) {  fatalError("init(coder:) has not been implemented")}}

是不是还觉得缺少点什么?没错,我们的headvIEw是不是还没整啊?
接下来呢,我们看下UICollectionVIEw的headvIEw该怎么设置
重点在这里!首先headvIEw要继承UICollectionReusableVIEw
然后我们这个.m文件里面并没有看到overrIDe func vIEwDIDLoad()这样的方法
那我们怎么办呢?
接下来就看我的了
我们点到我们继承的UICollectionReusableVIEw里面去看里面有些什么方法
功夫不负有心人,��终于找到了一个可以让我们用的方法

overrIDe func applyLayoutAttributes(layoutAttributes: UICollectionVIEwLayoutAttributes!){}

我们可以把要自定义的UI 请求数据什么的都放这方法里面
也就相当于我们VC里面的overrIDe func vIEwDIDLoad()这个方法
教程到结束
有任何问题可以留言,定期抽时间回复

版权归©Bison所有 任何转载请标明出处!

> 更多经验请点击


>最终效果图如下

总结

以上是内存溢出为你收集整理的Swift纯代码走进UICollectionView全部内容,希望文章能够帮你解决Swift纯代码走进UICollectionView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1084249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存