swift中collectionView的简单用法
Posted zhangxianhongx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift中collectionView的简单用法相关的知识,希望对你有一定的参考价值。
之前写过OC中collectionView的用法,现在再看看swift中collectionView的用法,有兴趣的朋友,可以两者前后比较下区别,swift现在没有稳定下来,语法更新的比较快,但是它核心的一些东西,已经定型了。这些还是靠读者们自己去挖掘吧。
//这里签署数据源和代理,此时不需要引入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; // let x = self._collectionView!.contentOffset.x/kScreenW; // UIView.animateWithDuration(0.25) { () -> Void in // // self._collectionView?.contentOffset = CGPointMake(kScreenW * CGFloat(Int64(x)) + 10 * CGFloat(Int64(x)), 0); // }; } 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"); loadData(); } //data Source func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1; } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if self._dataArray==nil{ return 0; } return (self._dataArray?.count)!; } 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"; }
//数据解析 let dic1 = self._dataArray![indexPath.item] as! NSDictionary; let dic = dic1.objectForKey("content") as! NSDictionary; let title = dic.objectForKey("title") as! String; let desc = dic.objectForKey("desc") as! String; lab?.text = title + "\n" + desc; return cell; } func scrollViewDidEndDecelerating(scrollView: UIScrollView) { // let x = scrollView.contentOffset.x/kScreenW; // UIView.animateWithDuration(0.25) { () -> Void in // self._collectionView?.contentOffset = CGPointMake(kScreenW * CGFloat(Int64(x)) + 10 * CGFloat(Int64(x)), 0); // }; } //delegate func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let viewC = SecAmonViewController(); let dic1 = self._dataArray![indexPath.item] as! NSDictionary; let dic = dic1.objectForKey("content") as! NSDictionary; viewC.title = dic.objectForKey("title") as? String; self.navigationController?.pushViewController(viewC, animated: true); } //自定义的数据请求方法 func loadData(){ CoreWork.homeOneSecondThirdList { (responsObject) -> Void in let firDic = responsObject; if firDic.isKindOfClass(NSDictionary) == false{ return; } let secondDic = firDic.objectForKey("data") as! NSDictionary; self._dataArray = secondDic.objectForKey("md")?.objectForKey("module") as? NSArray; self._collectionView?.reloadData(); } } }
以上是关于swift中collectionView的简单用法的主要内容,如果未能解决你的问题,请参考以下文章
Swift 中组合的 TableViews 和 CollectionViews
以编程方式从 collectionView Cell didSelectItemAt 推送到新的 ViewController - swift
CollectionView 在 StackView (Swift) 中消失
在 Swift 3 中使用 CGSize 的 collectionView 的 100% 宽度
在 iOS Swift 中,在 Swift 4 中的 TableView 中为 CollectionView 集成不同的数组