UICollectionView基礎
//MARK: - 屬性
//1.collectionView
var collectionView:UICollectionView? = nil
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
//0.創(chuàng)建UICollectionViewFlowLayout對象赖舟,這個對象是專門負責collectionView的布局
//UICollectionViewFlowLayout:UICollectionViewLayout
let layout = UICollectionViewFlowLayout()
//a.設置每個cell的大小(有對應的協(xié)議方法來設置每個cell的大小)
//layout.itemSize = CGSizeMake(100, 150)
//b.設置每個cell之間的最小間距(行間距和列間距)
layout.minimumLineSpacing = 0 //設置這個屬性就是直接設置了每一行cell之間的間距
layout.minimumInteritemSpacing = 0
//c.設置滾動方向(默認是上下滾動)
layout.scrollDirection = .Vertical
//1.創(chuàng)建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.創(chuàng)建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.去復用池中查看是否有可以復用的cell,如果有就直接返回那個可以復用的cell;沒有就自動創(chuàng)建一個新的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無限循環(huán)
//MARK: - 屬性
//1.collectionView
var collectionView: UICollectionView? = nil
//2.數據源數組
lazy var dataArray:[UIImage] = {
var tempArray = [UIImage]()
//通過for循環(huán)創(chuàng)建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.創(chuàng)建layout
let layout = UICollectionViewFlowLayout()
//a.設置最小間距
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
//b.設置滾動方向
layout.scrollDirection = .Horizontal
//2.創(chuàng)建對象
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.創(chuàng)建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.創(chuàng)建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.創(chuàng)建UICollectionViewFlowLayout對象钥庇,這個對象是專門負責collectionView的布局
//UICollectionViewFlowLayout:UICollectionViewLayout
let layout = UICollectionViewFlowLayout()
//a.設置每個cell的大小(有對應的協(xié)議方法來設置每個cell的大小)
//layout.itemSize = CGSizeMake(100, 150)
//b.設置每個cell之間的最小間距(行間距和列間距)
layout.minimumLineSpacing = 0 //設置這個屬性就是直接設置了每一行cell之間的間距
layout.minimumInteritemSpacing = 0
//c.設置滾動方向(默認是上下滾動)
layout.scrollDirection = .Vertical
//1.創(chuàng)建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.創(chuàng)建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1.去復用池中查看是否有可以復用的cell,如果有就直接返回那個可以復用的cell;沒有就自動創(chuàng)建一個新的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: - 創(chuàng)建headerView
//3.設置headerView和footerView
//參數2:類型 ->標識當前設置的headerView還是footerView
//參數3:indexPath位置
//返回值:創(chuàng)建好的headerView/footerView
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
//UICollectionView中創(chuàng)建headerView的過程和創(chuàng)建cell的過程基本一樣
//1.去復用池中查找是否有可以復用的View,如果找到了就直接返回嗦枢。沒有找到就會自動去創(chuàng)建一個新的view.(在使用這個方法前需要注冊view)
//參數1:確定當前要創(chuàng)建的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
}