歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Swift中UICollectionView的簡單用法

Swift中UICollectionView的簡單用法

日期:2017/3/1 9:15:06   编辑:Linux編程

之前寫過OC中collectionView的用法,現在再看看swift中collectionView的用法,有興趣的朋友,可以兩者前後比較下區別,swift現在沒有穩定下來,語法更新的比較快,但是它核心的一些東西,已經定型了。這些還是靠讀者們自己去挖掘吧。

總得來說,swift無論是在語法上,還是在其書寫內容上,和OC有著千絲萬縷的聯系,但又不完全一樣,有的地方,進行了優化。當然這些都是筆者個人理解,畢竟也是從頭開始研究這個語言,可能有些不對的地方,也希望各位能夠指出。

//簽署數據源和代理,此時不需要引入layout的代理。

//此屬性設置為false可以解決由於scrollview引起的界面bug

self.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 Source

func 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;

}

//delegate

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

}

}

下面關於Swift的內容你可能也喜歡

Ubuntu 14.04.4 下安裝 Swift 2.2.1 http://www.linuxidc.com/Linux/2016-05/131249.htm

Ubuntu 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 的詳細介紹:請點這裡

Copyright © Linux教程網 All Rights Reserved