collectionView

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1. 系統(tǒng)默認(rèn)提供布局方式: 多行多列布局
        let layout = UICollectionViewFlowLayout()
//        //2. 設(shè)置Cell的大小
        layout.itemSize = CGSize(width: self.view.bounds.size.width, height: 200)
//        //3. 滾動方向
        layout.scrollDirection = .Horizontal
        //設(shè)置上下左右的間距
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
//        //cell的最小間距
        layout.minimumInteritemSpacing = 0
//        //行間距
        layout.minimumLineSpacing = 0
        
        let rect = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
        collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.whiteColor()
        //顯示滾動條
        collectionView.showsHorizontalScrollIndicator = false
        
        //分頁顯示
        collectionView.pagingEnabled = true
        
        //注冊cell
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        //注冊section header/footer
        collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
        collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
        
        collectionView.dataSource = self
        collectionView.delegate = self
        self.view.addSubview(collectionView)
        
        NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTimer(_:)), userInfo: nil, repeats: true)
    }
    
    func didTimer(timer: NSTimer) {
        //兩種表示方法
        //NSIndexPath
        //位置: frame.origin
        //獲取內(nèi)容的偏移量
        let offset = collectionView.contentOffset
        //根據(jù)位置獲取Cell
        let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x, y: 0))
        if indexPath!.item == 4 {
            //滾動某個Cell到可見區(qū)域
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: .Left, animated: false)
            
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 1, inSection: 0), atScrollPosition: .Left, animated: true)
        }
        else {
            collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item + 1, inSection: 0), atScrollPosition: .Left, animated: true)
        }
    }
    //設(shè)置Section的個數(shù)
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 4
    }

    //Section中Cell的個數(shù)
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        if indexPath.item % 2 == 0 {
            cell.contentView.backgroundColor = UIColor.redColor()
        }
        else {
            cell.contentView.backgroundColor = UIColor.blueColor()
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("\(indexPath.section):\(indexPath.item)")
    }
    
    //section header
    //section footer
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        //header
        if kind == UICollectionElementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath)
            
            header.backgroundColor = UIColor.blueColor()
            return header
        }
        else {
            let footer = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath)
            
            footer.backgroundColor = UIColor.cyanColor()
            return footer
        }
    }
    
    //cell大小
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 10 * (indexPath.item + 1), height: 10 * (indexPath.item + 1))
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 100, height: 200)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 100, height: 200)
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市秘遏,隨后出現(xiàn)的幾起案子儡司,更是在濱河造成了極大的恐慌,老刑警劉巖渐尿,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件醉途,死亡現(xiàn)場離奇詭異,居然都是意外死亡砖茸,警方通過查閱死者的電腦和手機(jī)隘擎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來凉夯,“玉大人货葬,你說我怎么就攤上這事【⒐唬” “怎么了震桶?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長征绎。 經(jīng)常有香客問我蹲姐,道長,這世上最難降的妖魔是什么炒瘸? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任淤堵,我火速辦了婚禮,結(jié)果婚禮上顷扩,老公的妹妹穿的比我還像新娘拐邪。我一直安慰自己,他們只是感情好隘截,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布扎阶。 她就那樣靜靜地躺著汹胃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪东臀。 梳的紋絲不亂的頭發(fā)上着饥,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天,我揣著相機(jī)與錄音惰赋,去河邊找鬼宰掉。 笑死,一個胖子當(dāng)著我的面吹牛赁濒,可吹牛的內(nèi)容都是我干的轨奄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拒炎,長吁一口氣:“原來是場噩夢啊……” “哼挪拟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起击你,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤玉组,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后丁侄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惯雳,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年绒障,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吨凑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡户辱,死狀恐怖鸵钝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情庐镐,我是刑警寧澤恩商,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站必逆,受9級特大地震影響怠堪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜名眉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一粟矿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧损拢,春花似錦陌粹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽或舞。三九已至,卻和暖如春蒙幻,著一層夾襖步出監(jiān)牢的瞬間映凳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工邮破, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留诈豌,地道東北人。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓抒和,卻偏偏與公主長得像队询,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子构诚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評論 2 354

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