UICollectionView基礎(chǔ)

UICollectionView基礎(chǔ)

 //MARK: - 屬性
    //1.collectionView
    var collectionView:UICollectionView? = nil
    
    

    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //0.創(chuàng)建UICollectionViewFlowLayout對(duì)象,這個(gè)對(duì)象是專門(mén)負(fù)責(zé)collectionView的布局
        //UICollectionViewFlowLayout:UICollectionViewLayout
        let layout = UICollectionViewFlowLayout()
        
        //a.設(shè)置每個(gè)cell的大小(有對(duì)應(yīng)的協(xié)議方法來(lái)設(shè)置每個(gè)cell的大小)
        //layout.itemSize = CGSizeMake(100, 150)
        
        //b.設(shè)置每個(gè)cell之間的最小間距(行間距和列間距)
        layout.minimumLineSpacing = 0  //設(shè)置這個(gè)屬性就是直接設(shè)置了每一行cell之間的間距
        layout.minimumInteritemSpacing = 0
        
        //c.設(shè)置滾動(dòng)方向(默認(rèn)是上下滾動(dòng))
        layout.scrollDirection = .Vertical
        
    
        
        //1.創(chuàng)建UICollectionView對(duì)象
        self.collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        
        //2.添加到界面上  
      self.view.addSubview(self.collectionView!)
        
        //3.設(shè)置背景顏色(默認(rèn)的背景顏色是黑色)
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        
        //4.設(shè)置代理
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
        
        //5.注冊(cè)cell
        //參數(shù)1:指定cell的類型
        //參數(shù)2:復(fù)用id
        self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
    }
}

//UICollectionViewDelegateFlowLayout:UICollectionViewDelegate:UIScrollViewDelegate
extension ViewController:UICollectionViewDelegateFlowLayout{

    //1.設(shè)置每個(gè)cell的大小
    //可以讓不同的分組設(shè)置不同的大小
    //indexPath表示cell的位置 ->第幾組的第幾個(gè)
    //indexPath.section -> 組
    //indexPath.item -> 個(gè)
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        //方案1:確定每個(gè)cell的大小,和每一行cell的個(gè)數(shù)去計(jì)算間距
        //return CGSizeMake(100, 150)
        
        
        //方案2:確定每個(gè)cell的間距,和每一行cell的個(gè)數(shù),去計(jì)算cell的大小
        let width = self.view.bounds.width / 4
        return CGSizeMake(width, 150)
        
    }
    
    //2.設(shè)置每個(gè)分組到collectionView上左下右的邊距
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        //方案1:確定每個(gè)cell的大小,個(gè)每一行cell的個(gè)數(shù)去計(jì)算間距
        let margin = (self.view.bounds.width - 100*3)/4
        //return UIEdgeInsetsMake(margin, margin, margin, margin)
        
        //方案2:確定每個(gè)cell的間距,和每一行cell的個(gè)數(shù),去計(jì)算cell的大小
        //注意:每個(gè)cell之間的最小間距是10
        return UIEdgeInsetsMake(0, 0, 0, 0)
        
    }
    
    
    
}

//MARK: - collectionView DataSource
extension ViewController: UICollectionViewDataSource{

    //1.設(shè)置每個(gè)分組的cell的個(gè)數(shù)(默認(rèn)分組數(shù)為1)
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    
        return 100
    }
    
    //2.創(chuàng)建cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //1.去復(fù)用池中查看是否有可以復(fù)用的cell,如果有就直接返回那個(gè)可以復(fù)用的cell;沒(méi)有就自動(dòng)創(chuàng)建一個(gè)新的cell返回。(在這兒之前必須注冊(cè)cell)
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        
        //2.刷新數(shù)據(jù)
        //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無(wú)限循環(huán)

    //MARK: - 屬性
    //1.collectionView
    var collectionView: UICollectionView? = nil
    //2.數(shù)據(jù)源數(shù)組
    lazy var dataArray:[UIImage] = {
    
        var tempArray = [UIImage]()
        //通過(guò)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.設(shè)置最小間距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //b.設(shè)置滾動(dòng)方向
        layout.scrollDirection = .Horizontal
        
        //2.創(chuàng)建對(duì)象
        self.collectionView = UICollectionView.init(frame: CGRectMake(0, 0, self.view.bounds.width, 200), collectionViewLayout:layout)
        //3.添加到界面上
      self.view.addSubview(self.collectionView!)
        //4.設(shè)置代理
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
        
        //5.注冊(cè)cell
        //a.注冊(cè)手寫(xiě)和storyBoard的cell
        //self.collectionView?.registerClass(ManCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        //b.注冊(cè)xib的cell
        let nib = UINib.init(nibName: "XibCollectionViewCell", bundle: nil)
        self.collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
        
        //6.設(shè)置分頁(yè)
        self.collectionView?.pagingEnabled = true
        
        //7.設(shè)置偏移量
        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.設(shè)置cell的個(gè)數(shù)
    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.刷新數(shù)據(jù)
        cell.imageView.image = self.dataArray[indexPath.row%4]
        cell.textLabel.text = "第\(indexPath.row%4)張圖片"
        
        //3.返回?cái)?shù)據(jù)
        return cell
        
    }
    
    
//1.聲明所有的子視圖對(duì)應(yīng)的屬性
//2.在構(gòu)造方法中添加子視圖
//3.在layoutSubViews方法中去計(jì)算子視圖的frame

class ManCollectionViewCell: UICollectionViewCell {
    //MARK: - 屬性
    let imageView = UIImageView()
    
    //MARK: - 構(gòu)造方法
    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: - 計(jì)算frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView.frame = self.bounds
        
    }
    

UICollectionView分組


    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //0.創(chuàng)建UICollectionViewFlowLayout對(duì)象,這個(gè)對(duì)象是專門(mén)負(fù)責(zé)collectionView的布局
        //UICollectionViewFlowLayout:UICollectionViewLayout
        let layout = UICollectionViewFlowLayout()
        
        //a.設(shè)置每個(gè)cell的大小(有對(duì)應(yīng)的協(xié)議方法來(lái)設(shè)置每個(gè)cell的大小)
        //layout.itemSize = CGSizeMake(100, 150)
        
        //b.設(shè)置每個(gè)cell之間的最小間距(行間距和列間距)
        layout.minimumLineSpacing = 0  //設(shè)置這個(gè)屬性就是直接設(shè)置了每一行cell之間的間距
        layout.minimumInteritemSpacing = 0
        
        //c.設(shè)置滾動(dòng)方向(默認(rèn)是上下滾動(dòng))
        layout.scrollDirection = .Vertical
        
    
        
        //1.創(chuàng)建UICollectionView對(duì)象
        self.collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        
        //2.添加到界面上  
      self.view.addSubview(self.collectionView!)
        
        //3.設(shè)置背景顏色(默認(rèn)的背景顏色是黑色)
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        
        //4.設(shè)置代理
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
        
        //5.注冊(cè)cell
        //參數(shù)1:指定cell的類型
        //參數(shù)2:復(fù)用id
        self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        //6.注冊(cè)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.設(shè)置每個(gè)cell的大小
    //可以讓不同的分組設(shè)置不同的大小
    //indexPath表示cell的位置 ->第幾組的第幾個(gè)
    //indexPath.section -> 組
    //indexPath.item -> 個(gè)
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        //方案1:確定每個(gè)cell的大小,和每一行cell的個(gè)數(shù)去計(jì)算間距
        //return CGSizeMake(100, 150)
        
        
        //方案2:確定每個(gè)cell的間距,和每一行cell的個(gè)數(shù),去計(jì)算cell的大小
        let width = self.view.bounds.width / 4
        return CGSizeMake(width, 150)
        
    }
    
    //2.設(shè)置每個(gè)分組到collectionView上左下右的邊距
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        //方案1:確定每個(gè)cell的大小,個(gè)每一行cell的個(gè)數(shù)去計(jì)算間距
        let margin = (self.view.bounds.width - 100*3)/4
        //return UIEdgeInsetsMake(margin, margin, margin, margin)
        
        //方案2:確定每個(gè)cell的間距,和每一行cell的個(gè)數(shù),去計(jì)算cell的大小
        //注意:每個(gè)cell之間的最小間距是10
        return UIEdgeInsetsMake(0, 0, 20, 0)
        
    }
    
    //MARK: - 設(shè)置header的大小
    //3.設(shè)置header的大小
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
    
        return CGSizeMake(300, 60)
        
    }
    
    
    
}

//MARK: - collectionView DataSource
extension ViewController: UICollectionViewDataSource{
    
    //0.設(shè)置分組數(shù)(默認(rèn)是1)
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
    
        return 3
        
    }

    //1.設(shè)置每個(gè)分組的cell的個(gè)數(shù)(默認(rèn)分組數(shù)為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.去復(fù)用池中查看是否有可以復(fù)用的cell,如果有就直接返回那個(gè)可以復(fù)用的cell;沒(méi)有就自動(dòng)創(chuàng)建一個(gè)新的cell返回。(在這兒之前必須注冊(cè)cell)
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        
        //2.刷新數(shù)據(jù)
        //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.設(shè)置headerView和footerView
    //參數(shù)2:類型 ->標(biāo)識(shí)當(dāng)前設(shè)置的headerView還是footerView
    //參數(shù)3:indexPath位置
    //返回值:創(chuàng)建好的headerView/footerView
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
        //UICollectionView中創(chuàng)建headerView的過(guò)程和創(chuàng)建cell的過(guò)程基本一樣
        //1.去復(fù)用池中查找是否有可以復(fù)用的View,如果找到了就直接返回。沒(méi)有找到就會(huì)自動(dòng)去創(chuàng)建一個(gè)新的view.(在使用這個(gè)方法前需要注冊(cè)view)
        //參數(shù)1:確定當(dāng)前要?jiǎng)?chuàng)建的headerView還是footerView
        //UICollectionElementKindSectionHeader ->headerView
        //UICollectionElementKindSectionFooter ->footerView
        //參數(shù)2.復(fù)用id
        //參數(shù)3:header的位置
        let reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "view", forIndexPath: indexPath) as! XibReusableView
        
        //2.刷新數(shù)據(jù)
        let titles = ["周星馳系列","經(jīng)典電影","每一份父愛(ài)"]
        reuseView.titleLabel.text = titles[indexPath.section]
        
        
        //3.返回view
        return reuseView
        
        
    }


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容