码迷,mamicode.com
首页 > 编程语言 > 详细

swift中collectionView的简单用法

时间:2016-05-26 10:19:12      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

之前写过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的简单用法

标签:

原文地址:http://www.cnblogs.com/zxh-iOS/p/5529674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!