import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var nameArr:[String]?
var picArr :[String]?
var collect : UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
picArr = ["1","2","3","4","5","6"]
nameArr = ["一","二","三","四","五","六"]
collect = UICollectionView(frame: self.view.frame, collectionViewLayout: UICollectionViewFlowLayout())
collect?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
collect?.delegate = self
collect?.dataSource = self
self.view.addSubview(collect!)
// Do any additional setup after loading the view, typically from a nib.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (picArr?.count)!
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
let img = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
img.image = UIImage(named: (picArr?[indexPath.item])!)
cell.contentView.addSubview(img)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("\(nameArr?[indexPath.item])")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
使用网格(UICollectionView)进行流布局
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 目录[-]iOS流布局UICollectionView系列一——初识与简单使用UICollectionView一、...
- iOS流布局UICollectionView系列二——UICollectionView的代理方法 一、引言 在上一...
- 自定义 cell 继承于 UICollectionViewCell 的时候 1.初始化,在这里可以做一些添加子视图...