声明属性
var collectionView :UICollectionView?
var flowLayout :UICollectionViewFlowLayout?
var arrayM :NSMutableArray?
var image :UIImage?
var imageView :UIImageView?
createCollectionView
func createCollectionView() {
// 将属性初始化
self.flowLayout = UICollectionViewFlowLayout()
self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: self.flowLayout!)
self.arrayM = NSMutableArray()
// 准备数据
for i in 0..<9 {
self.image = UIImage(named: "\(i).jpg")
self.arrayM!.addObject(self.image!)
}
// 设置数据源和代理
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
// 流式布局时设置集合视图单元格大小
self.flowLayout!.itemSize = CGSizeMake(75, 75)
// 注册cell
self.collectionView!.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "collectionReuse")
// 将集合视图添加到视图中
self.collectionView?.backgroundColor = Theme.BackgroundColor
self.view.addSubview(self.collectionView!)
}
UICollectionViewDataSource & UICollectionViewDelegateFlowLayout
extension ViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
// MARK:- UICollectionViewDataSource
// 定义展示的Section的个数
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
// 定义展示的UICollectionViewCell的个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayM!.count
}
// 每个UICollectionView展示的内容
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let Reuseidentifier:NSString = "collectionReuse"
var cell:UICollectionViewCell? = collectionView.dequeueReusableCellWithReuseIdentifier(Reuseidentifier as String, forIndexPath: indexPath) as UICollectionViewCell
if cell == nil {
print("cell is nil")
cell = UICollectionViewCell()
}
self.imageView = UIImageView(frame: CGRectMake(0, 0, 75, 75))
self.imageView?.image = self.arrayM!.objectAtIndex(indexPath.item) as? UIImage
let label:UILabel? = UILabel(frame: CGRectMake(20,80,75,20))
label!.text = "{0,\(indexPath.item)}"
cell!.addSubview(imageView!)
cell!.addSubview(label!)
return cell!
}
// MARK:- UICollectionViewDelegateFlowLayout
// 设置cell和视图边的间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 20, 10, 20)
}
// 设置每一个cell最小行间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 30
}
// 设置每一个cell的列间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10
}
// 某个Cell被选择的事件处理
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
print("选中1区第\(indexPath.row)行")
} else if indexPath.section == 1 {
print("选中2区第\(indexPath.row)行")
} else {
print("选中3区第\(indexPath.row)行")
}
}
}
运行效果
网友评论