• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift中collectionView的简单用法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

之前写过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(); } } }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift - 制作一个在线流媒体音乐播放器(使用StreamingKit库)发布时间:2022-07-13
下一篇:
Swift中@available和#available发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap