天天看點

swift UICollectionView的使用

之前研究swift,用到了UICollectionView,在此拿出來和大家分享一下。。。

下面是主要代碼

/**
     建立collectionView
     */
    func createTypeCollection() {
        let layout = UICollectionViewFlowLayout.init()
        self.typeCollection = UICollectionView.init(frame: CGRectMake(, MAX_HEIGHT(self.toolBtn!), SCREEN_WIDTH, SCREEN_HEIGHT - MAX_HEIGHT(self.toolBtn!)), collectionViewLayout: layout)
        self.typeCollection?.backgroundColor = UIColor.clearColor()
        //注冊CollectionViewCell
        self.typeCollection?.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"cell")
        //注冊headerView
        self.typeCollection?.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "cell")
        self.typeCollection?.delegate = self;
        self.typeCollection?.dataSource = self;
        self.view.addSubview(self.typeCollection!)
    }

    /**
     設定組别

     - parameter collectionView:

     - returns: 傳回幾個組别
     */
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return self.typeArray.count
    }

    /**
     設定每個組别内的分類數量

     - parameter collectionView:
     - parameter section:        目前組别

     - returns: 傳回個數
     */
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let dic = self.typeArray[section] as! NSDictionary
        let array = dic["items"] as! Array<String>
        return array.count
    }

    /**
     設定collectionView headerView的高度

     - parameter collectionView:
     - parameter collectionViewLayout:
     - parameter section:              第幾個

     - returns: 傳回大小
     */
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
        return CGSize.init(width: SCREEN_WIDTH, height: )
    }

    /**
     設定每一個itme的大小

     - parameter collectionView:
     - parameter collectionViewLayout:
     - parameter indexPath:

     - returns: 大小
     */
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        let dic = self.typeArray[indexPath.section] as! NSDictionary
        let array = dic["items"] as! Array<String>
        let size = getTextRectSize(array[indexPath.row], number: , width: )
        return CGSize.init(width: size.width + , height: )
    }

    /**
     設定上下左右間距

     - parameter collectionView:
     - parameter collectionViewLayout:
     - parameter section:

     - returns: 傳回間距
     */
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        return UIEdgeInsets.init(top: , left: , bottom: , right: )
    }

    /**
     設定headerView

     - parameter collectionView:
     - parameter kind:
     - parameter indexPath:      第幾個

     - returns: 傳回
     */
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{

        let dic = self.typeArray[indexPath.section] as! NSDictionary

        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "cell", forIndexPath: indexPath)
        for v in headerView.subviews {
            v.removeFromSuperview()
        }
        let headerLabel = UILabel.init(frame: CGRectMake(, , SCREEN_WIDTH, ))
        headerLabel.textColor = BG_COLOR(, g: , b: , a: )
        headerLabel.font = UIFont.init(name: MY_FONT, size: )
        headerLabel.text = dic["title"] as? String
        headerView.addSubview(headerLabel)
        return headerView
    }

    /**
     設定cell顯示内容以及複用

     - parameter collectionView:
     - parameter indexPath:

     - returns: 傳回cell
     */
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
        let dic = self.typeArray[indexPath.section] as? NSDictionary
        let array = dic!["items"] as? Array<String>
        let title = array![indexPath.row] as String
        cell.refreshLabel(title)
        if self.selectCellPath == indexPath {
            cell.selectCellType()
        }else{
            cell.cancelSelectCellType()
        }
        return cell
    }

    /**
     選擇collectionView cell的方法

     - parameter collectionView:
     - parameter indexPath:
     */
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.selectCellPath = indexPath
        self.typeCollection?.reloadData()
    }
    /**
     可以擷取上一個選擇的cell

     - parameter collectionView:
     - parameter indexPath:
     */
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    }
           

下面是設定資料的代碼(本地資料)

/**
     建立資料數組
     */
    func createTypeData() {
        self.typeArray = [
            [
                "title":"文學",
                "items":[
                    "小說","漫畫","青春文學","随筆","現當代詩","戲劇","傳記","古詩詞","外國詩歌","藝術","攝影"
                ]
            ],
            [
                "title":"人文社科",
                "items":[
                    "曆史","文化","古籍","心理學","哲學/宗教","政治/軍事","社會科學","法律"
                ]
            ],
            [
                "title":"生活",
                "items":[
                    "休閑/愛好","孕産/胎教","烹饪/美食","時尚/美妝","旅遊/地圖","家庭/家居","親子/家教","兩性關系","育兒/早教","保健/養生","體育/運動","手工/DIY"
                ]
            ],
            [
                "title":"經管",
                "items":[
                    "管理","投資","理财","經濟"
                ]
            ],
            [
                "title":"科技",
                "items":[
                    "科普讀物","建築","醫學","計算機/網絡","農業/林業","自然科學","工業技術"
                ]
            ],
            [
                "title":"網絡流行",
                "items":[
                    "玄幻/奇幻","武俠/仙俠","都市/職業","曆史/軍事","遊戲/競技","科幻/靈異","言情"
                ]
            ],

        ]
    }