UICollectionView基础
//MARK: - 属性
//1.collectionView
var collectionView:UICollectionView? = nil
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
//0.创建UICollectionViewFlowLayout对象,这个对象是专门负责collectionView的布局
//UICollectionViewFlowLayout:UICollectionViewLayout
let layout = UICollectionViewFlowLayout()
//a.设置每个cell的大小(有对应的协议方法来设置每个cell的大小)
//layout.itemSize = CGSizeMake(100, 150)
//b.设置每个cell之间的最小间距(行间距和列间距)
layout.minimumLineSpacing = 0 //设置这个属性就是直接设置了每一行cell之间的间距
layout.minimumInteritemSpacing = 0
//c.设置滚动方向(默认是上下滚动)
layout.scrollDirection = .Vertical
//1.创建UICollectionView对象
self.collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
//2.添加到界面上
self.view.addSubview(self.collectionView!)
//3.设置背景颜色(默认的背景颜色是黑色)
self.collectionView?.backgroundColor = UIColor.whiteColor()
//4.设置代理
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
//5.注册cell
//参数1:指定cell的类型
//参数2:复用id
self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
}
//UICollectionViewDelegateFlowLayout:UICollectionViewDelegate:UIScrollViewDelegate
extension ViewController:UICollectionViewDelegateFlowLayout{
//1.设置每个cell的大小
//可以让不同的分组设置不同的大小
//indexPath表示cell的位置 ->第几组的第几个
//indexPath.section -> 组
//indexPath.item -> 个
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
//方案1:确定每个cell的大小,和每一行cell的个数去计算间距
//return CGSizeMake(100, 150)
//方案2:确定每个cell的间距,和每一行cell的个数,去计算cell的大小
let width = self.view.bounds.width / 4
return CGSizeMake(width, 150)
}
//2.设置每个分组到collectionView上左下右的边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
//方案1:确定每个cell的大小,个每一行cell的个数去计算间距
let margin = (self.view.bounds.width - 100*3)/4
//return UIEdgeInsetsMake(margin, margin, margin, margin)
//方案2:确定每个cell的间距,和每一行cell的个数,去计算cell的大小
//注意:每个cell之间的最小间距是10
return UIEdgeInsetsMake(0, 0, 0, 0)
}
}
//MARK: - collectionView DataSource
extension ViewController: UICollectionViewDataSource{
//1.设置每个分组的cell的个数(默认分组数为1)
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 100
}
//2.创建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.去复用池中查看是否有可以复用的cell,如果有就直接返回那个可以复用的cell;没有就自动创建一个新的cell返回。(在这儿之前必须注册cell)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
//2.刷新数据
//CGFloat(arc4random()%256)/255
cell.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1)
//3.返回cell
return cell
}
UICollectionView无限循环
//MARK: - 属性
//1.collectionView
var collectionView: UICollectionView? = nil
//2.数据源数组
lazy var dataArray:[UIImage] = {
var tempArray = [UIImage]()
//通过for循环创建4张图片
for item in 1...4{
let image = UIImage.init(named: "35_\(item).jpg")
tempArray.append(image!)
}
return tempArray
}()
override func viewDidLoad() {
super.viewDidLoad()
//1.创建layout
let layout = UICollectionViewFlowLayout()
//a.设置最小间距
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
//b.设置滚动方向
layout.scrollDirection = .Horizontal
//2.创建对象
self.collectionView = UICollectionView.init(frame: CGRectMake(0, 0, self.view.bounds.width, 200), collectionViewLayout:layout)
//3.添加到界面上
self.view.addSubview(self.collectionView!)
//4.设置代理
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
//5.注册cell
//a.注册手写和storyBoard的cell
//self.collectionView?.registerClass(ManCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//b.注册xib的cell
let nib = UINib.init(nibName: "XibCollectionViewCell", bundle: nil)
self.collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
//6.设置分页
self.collectionView?.pagingEnabled = true
//7.设置偏移量
self.collectionView?.contentOffset = CGPointMake(1000000/2*self.view.bounds.width, 0)
}
}
//MARK: - delegate
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return collectionView.frame.size
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(0, 0, 0, 0)
}
}
//MARK: - dataSource
extension ViewController:UICollectionViewDataSource{
//1.设置cell的个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1000000
}
//2.创建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.创建cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! XibCollectionViewCell
//2.刷新数据
cell.imageView.image = self.dataArray[indexPath.row%4]
cell.textLabel.text = "第\(indexPath.row%4)张图片"
//3.返回数据
return cell
}
//1.声明所有的子视图对应的属性
//2.在构造方法中添加子视图
//3.在layoutSubViews方法中去计算子视图的frame
class ManCollectionViewCell: UICollectionViewCell {
//MARK: - 属性
let imageView = UIImageView()
//MARK: - 构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.contentView.addSubview(imageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - 计算frame
override func layoutSubviews() {
super.layoutSubviews()
imageView.frame = self.bounds
}
UICollectionView分组
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
//0.创建UICollectionViewFlowLayout对象,这个对象是专门负责collectionView的布局
//UICollectionViewFlowLayout:UICollectionViewLayout
let layout = UICollectionViewFlowLayout()
//a.设置每个cell的大小(有对应的协议方法来设置每个cell的大小)
//layout.itemSize = CGSizeMake(100, 150)
//b.设置每个cell之间的最小间距(行间距和列间距)
layout.minimumLineSpacing = 0 //设置这个属性就是直接设置了每一行cell之间的间距
layout.minimumInteritemSpacing = 0
//c.设置滚动方向(默认是上下滚动)
layout.scrollDirection = .Vertical
//1.创建UICollectionView对象
self.collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
//2.添加到界面上
self.view.addSubview(self.collectionView!)
//3.设置背景颜色(默认的背景颜色是黑色)
self.collectionView?.backgroundColor = UIColor.whiteColor()
//4.设置代理
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
//5.注册cell
//参数1:指定cell的类型
//参数2:复用id
self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//6.注册view
//self.collectionView?.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "view")
self.collectionView?.registerNib(UINib.init(nibName: "XibReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "view")
}
}
//MARK: - delegate
//UICollectionViewDelegateFlowLayout:UICollectionViewDelegate:UIScrollViewDelegate
extension ViewController:UICollectionViewDelegateFlowLayout{
//1.设置每个cell的大小
//可以让不同的分组设置不同的大小
//indexPath表示cell的位置 ->第几组的第几个
//indexPath.section -> 组
//indexPath.item -> 个
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
//方案1:确定每个cell的大小,和每一行cell的个数去计算间距
//return CGSizeMake(100, 150)
//方案2:确定每个cell的间距,和每一行cell的个数,去计算cell的大小
let width = self.view.bounds.width / 4
return CGSizeMake(width, 150)
}
//2.设置每个分组到collectionView上左下右的边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
//方案1:确定每个cell的大小,个每一行cell的个数去计算间距
let margin = (self.view.bounds.width - 100*3)/4
//return UIEdgeInsetsMake(margin, margin, margin, margin)
//方案2:确定每个cell的间距,和每一行cell的个数,去计算cell的大小
//注意:每个cell之间的最小间距是10
return UIEdgeInsetsMake(0, 0, 20, 0)
}
//MARK: - 设置header的大小
//3.设置header的大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSizeMake(300, 60)
}
}
//MARK: - collectionView DataSource
extension ViewController: UICollectionViewDataSource{
//0.设置分组数(默认是1)
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
return 3
}
//1.设置每个分组的cell的个数(默认分组数为1)
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
switch section {
case 0:
return 7
case 1:
return 5
default:
return 11
}
}
//2.创建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.去复用池中查看是否有可以复用的cell,如果有就直接返回那个可以复用的cell;没有就自动创建一个新的cell返回。(在这儿之前必须注册cell)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
//2.刷新数据
//CGFloat(arc4random()%256)/255
cell.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1)
//3.返回cell
return cell
}
//MARK: - 创建headerView
//3.设置headerView和footerView
//参数2:类型 ->标识当前设置的headerView还是footerView
//参数3:indexPath位置
//返回值:创建好的headerView/footerView
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
//UICollectionView中创建headerView的过程和创建cell的过程基本一样
//1.去复用池中查找是否有可以复用的View,如果找到了就直接返回。没有找到就会自动去创建一个新的view.(在使用这个方法前需要注册view)
//参数1:确定当前要创建的headerView还是footerView
//UICollectionElementKindSectionHeader ->headerView
//UICollectionElementKindSectionFooter ->footerView
//参数2.复用id
//参数3:header的位置
let reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "view", forIndexPath: indexPath) as! XibReusableView
//2.刷新数据
let titles = ["周星驰系列","经典电影","每一份父爱"]
reuseView.titleLabel.text = titles[indexPath.section]
//3.返回view
return reuseView
}