多了不多说前几天在做项目用到了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")
}
//看本文章的伙伴们不好意思了,本人文采不好,技术一般 有牛人发现什么错误问题多多指正,本文章是给IOS 新手借鉴希望一起共同成长 有问题或者有错误加QQ78807153 多谢指正!