发布于 2015-12-28 21:35:55 | 387 次阅读 | 评论: 0 | 来源: 网友投递
Swift编程语言
SWIFT,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序。
多了不多说前几天在做项目用到了UIcollView 实现跟OC 大同小异,有个别问题需要注意。(时间问题没有排版忘见谅)好不多说进入正题!
在使用用UIcollectionView跟TableView 一样需要遵循两个代理方法UICollectionViewDataSource,UICollectionViewDelegate
接下来是实现UICollectionViewFlowLayout 下面是代码:
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSizeMake((device_width-7)/2, 210)
flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical//设置垂直显示
flowLayout.sectionInset = UIEdgeInsetsMake(0, 1, 0, 1)//设置边距
flowLayout.minimumLineSpacing = 0.0;//每个相邻layout的上下
flowLayout.minimumInteritemSpacing = 0.0;//每个相邻layout的左右
flowLayout.headerReferenceSize = CGSizeMake(0, 0);
继续实现UICollectionView
self.floorResultCollectionView = UICollectionView(frame: CGRectMake(0,self.start_y, device_width, device_hight), collectionViewLayout: flowLayout)
self.floorResultCollectionView.backgroundColor = UIColor.whiteColor()
self.floorResultCollectionView.alwaysBounceVertical = true
self.floorResultCollectionView.delegate = self
self.floorResultCollectionView.dataSource = self
self.view.addSubview(self.floorResultCollectionView)
self.floorResultCollectionView.registerClass(FloorCollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 这个是关键一定要这注册下
然后实现代理方法 这里主要看看cell的使用
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as FloorCollectionViewCell
cell.textLabel.text = "Text"
cell.imageView.image = UIImage(named: "star")
return cell
}
伙计这基本上完成了 在看看cell是怎么自定义把!
override init(frame: CGRect) {
super.init(frame: frame)
}
在init 里面添加你要实现的样式 ,不过在Code6.1会给你个错误提示,只要是实现
init(coder aDecoder: NSCoder)问题就解决了
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}