iOS UICollectionView基础使用方法
来源:互联网 发布:3dmax软件培训 编辑:程序博客网 时间:2024/06/08 17:17
CollectionView 80%与tableView一样。很易学,更灵活
import UIKit
import Foundation
class ViewController:UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
//假设cell高度
let collentionCellHeight:CGFloat =110
//屏幕宽度:
let width =UIScreen.main.bounds.width
var collection:UICollectionView!
//数据源
var dataArr= [“1”,“23”,“4”,“21”,“2”,“32”,“5646”,“3234”,“435”,“242”]
overridefunc viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =UIColor.white
方法在下面
initCollection()
}
func initCollection(){
//与tableView不同 一:必有layout
let lay =UICollectionViewFlowLayout()
//每个元素大小:
lay.itemSize =CGSize(width:width/3, height:collentionCellHeight)
(一会有定义不同cell不同高度)
lay.minimumInteritemSpacing =20//行间距
//构造collectionView
//collection
collection =UICollectionView(frame:view.bounds,collectionViewLayout: lay)
//俩协议
collection.delegate =self
collection.dataSource =self
collection.backgroundColor = .white
//注册cell,和tableView一样
//用collectionView时候,cell都是自定义的,自定义的ELCollectionViewCell类在最下面。现在先直接写
collection.register(ELCollectionViewCell.self,forCellWithReuseIdentifier:“cellID”)
view.addSubview(collection)
}
}
// collectionView的代理方法,和tableView差不多
1.返回行数
func collectionView(_ collectionView:UICollectionView, numberOfItemsInSection section:Int) -> Int {
returndataArr.count
}
2. cell内容func collectionView(_ collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell {
let cell:ELCollectionViewCell =collection.dequeueReusableCell(withReuseIdentifier:“cellID”, for: indexPath)as? ELCollectionViewCell
let model =dataArr[indexPath.item]
cell.labelName?.text = “name:” + model
cell.labelNum?.text =model
return cell
}
func collectionView(_ collectionView:UICollectionView, didSelectItemAt indexPath:IndexPath) {
//业务需求点击实现的东西自定
}
还有一个协议,是定义详细布局的,每个cell的高度之类的,叫 UICollectionViewDelegateFlowLayout,实现里面方法没什么好说的
}
class ELCollectionViewCell: UICollectionViewCell {
let width =UIScreen.main.bounds.width
var labelName:UILabel?
var labelNum:UILabel?
overrideinit(frame:CGRect) {
super.init(frame: frame)
backgroundColor =UIColor.lightgray
//加点外观
layer.cornerRadius =15
layer.borderWidth =0.3
layer.borderColor =UIColor.green.cgColor
labelName =UILabel(frame:CGRect(x:20, y:10, width:100, height:50))
labelNum =UILabel(frame:CGRect(x:20, y: 60, width:100, height:50))
labelName?.font =UIFont.boldSystemFont(ofSize:23)
labelNum?.font =UIFont.systemFont(ofSize:12)
labelName?.textAlignment = .center
labelNum?.textAlignment = .center
addSubview(labelName!)
addSubview(labelNum!)
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
- iOS UICollectionView基础使用方法
- ios之基础 UICollectionView
- iOS开发基础 - UICollectionView
- IOS UICollectionView 的基础使用
- iOS开发中UICollectionView的使用方法
- UICollectionView使用方法
- UICollectionView使用方法
- IOS基础_ UICollectionView的简单使用
- iOS开发-UI控件:UICollectionView基础
- IOS中集合视图UICollectionView中DecorationView的简易使用方法
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- UICollectionView基础
- 疯狂Android讲义——P54-55丰富的表格视图(实现)
- css--3.盒子模型
- 【组合数】省赛CD题 SDUT_3895_fireworks SDUT_3896_HEX
- 数据在内存中的存储方式
- 设计模式之桥接模式+适配器模式Java实现
- iOS UICollectionView基础使用方法
- C语言程序设计(12)
- (转)后监管时代的资本市场,数字金融能否迎来大爆发?
- [李景山php] 深入理解PHP内核[读书笔记]--第三章:变量及数据类型--简略
- OAuth2.0
- 【bzoj1683】 [Usaco2005 Nov]City skyline 城市地平线
- 直接插入排序
- cmd 运行python版本切换
- 开始博客 2017.05.09