swift - UICollectionView

Jul-29-2019 15-41-54.gif

UICollectionView主要有以下幾個(gè)難點(diǎn)

1. UICollectionViewDelegateFlowLayout

使用系統(tǒng)的layout

 let collectionLayout = UICollectionViewFlowLayout.init()
 let rect = CGRect(x: 0, y: 0, width: screenW, height: screenH)
 let collectionView = UICollectionView(frame: rect, collectionViewLayout: collectionLayout)

2. 方法的運(yùn)用

必須實(shí)現(xiàn)方法

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCell(withReuseIdentifier: "UICollectionViewCell", for: indexPath)
        cell.backgroundColor = kColor(red: 53, green: 49, blue: 59)
        cell.layer.cornerRadius = 5;
        cell.layer.masksToBounds = true
        return cell
    }

header和footer 設(shè)置

    //   MARK: - 設(shè)定header和footer的方法,根據(jù)kind不同進(jìn)行不同的判斷即可
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let headerV = collectionView .dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerIdentifier", for: indexPath)
            headerV.backgroundColor = armColor()
            return headerV
        }else if kind == UICollectionView.elementKindSectionFooter{
            let footerV = collectionView .dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footIdentifier", for: indexPath)
            footerV.backgroundColor = armColor()
            return footerV
        }
        return UICollectionReusableView.init()
    }
    
    //    MARK: - headerView 高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize (width: screenW, height: 100)
    }
    
    //    MARK: - footerView 高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize (width: screenW, height: 100)
    }

item Size

    //    MARK: - item Size
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemW = (screenW-50)/3
        return CGSize(width: itemW, height: itemW)
    }

邊框距離

    //     MARK: - 邊框距離
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return  UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }

行最小間距

    //    MARK: - 行最小間距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }

列最小間距

    //    MARK: - 列最小間距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }

item點(diǎn)擊事件

    //    MARK: - item 點(diǎn)擊
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //        let cell = collectionView .cellForItem(at: indexPath)
        //        cell?.backgroundColor = armColor();
        
    }

item 高亮處理

    //    MARK: - 是否高亮
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool{
        return true
    }
    
    //    MARK: - 高亮顏色
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath){
        let cell = collectionView .cellForItem(at: indexPath)
        cell?.backgroundColor = armColor()
    }
    
    //    MARK: - 取消長(zhǎng)按顏色
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath){
        let cell = collectionView .cellForItem(at: indexPath)
        cell?.backgroundColor = kColor(red: 53, green: 49, blue: 59)
    }
    
    func armColor()->UIColor{
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        print("red:\(red),green:\(green),blue:\(blue)")
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末拜秧,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子境氢,更是在濱河造成了極大的恐慌,老刑警劉巖碰纬,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萍聊,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡悦析,警方通過(guò)查閱死者的電腦和手機(jī)寿桨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)强戴,“玉大人亭螟,你說(shuō)我怎么就攤上這事∽锰” “怎么了媒佣?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵匕累,是天一觀的道長(zhǎng)陵刹。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么衰琐? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任也糊,我火速辦了婚禮,結(jié)果婚禮上羡宙,老公的妹妹穿的比我還像新娘狸剃。我一直安慰自己,他們只是感情好狗热,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布钞馁。 她就那樣靜靜地躺著,像睡著了一般匿刮。 火紅的嫁衣襯著肌膚如雪僧凰。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,737評(píng)論 1 305
  • 那天熟丸,我揣著相機(jī)與錄音训措,去河邊找鬼。 笑死光羞,一個(gè)胖子當(dāng)著我的面吹牛绩鸣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播纱兑,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼呀闻,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了潜慎?” 一聲冷哼從身側(cè)響起总珠,我...
    開(kāi)封第一講書(shū)人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎勘纯,沒(méi)想到半個(gè)月后局服,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡驳遵,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年淫奔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堤结。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡唆迁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出竞穷,到底是詐尸還是另有隱情唐责,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布瘾带,位于F島的核電站鼠哥,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜朴恳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一抄罕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧于颖,春花似錦呆贿、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至同衣,卻和暖如春母蛛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背乳怎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工彩郊, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蚪缀。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓秫逝,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親询枚。 傳聞我的和親對(duì)象是個(gè)殘疾皇子违帆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

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