Swift中UICollectionView的简单用法
之前写过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的内容你可能也喜欢:
Swift 的详细介绍:请点这里