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)
}
}
collectionView
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來凉夯,“玉大人货葬,你說我怎么就攤上這事【⒐唬” “怎么了震桶?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長征绎。 經(jīng)常有香客問我蹲姐,道長,這世上最難降的妖魔是什么炒瘸? 我笑而不...
- 正文 為了忘掉前任淤堵,我火速辦了婚禮,結(jié)果婚禮上顷扩,老公的妹妹穿的比我還像新娘拐邪。我一直安慰自己,他們只是感情好隘截,可當(dāng)我...
- 文/花漫 我一把揭開白布扎阶。 她就那樣靜靜地躺著汹胃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪东臀。 梳的紋絲不亂的頭發(fā)上着饥,一...
- 文/蒼蘭香墨 我猛地睜開眼拒炎,長吁一口氣:“原來是場噩夢啊……” “哼挪拟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起击你,我...
- 序言:老撾萬榮一對情侶失蹤玉组,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后丁侄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惯雳,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年绒障,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吨凑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站必逆,受9級特大地震影響怠堪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜名眉,卻給世界環(huán)境...
- 文/蒙蒙 一粟矿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧损拢,春花似錦陌粹、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽或舞。三九已至,卻和暖如春蒙幻,著一層夾襖步出監(jiān)牢的瞬間映凳,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓抒和,卻偏偏與公主長得像队询,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子构诚,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 問題: 編譯執(zhí)行UICollectionView cellForItemAtIndexPath的 UICollec...
- 在UITableView的Cell里嵌套使用CollectionView場景里范嘱,如果在點(diǎn)擊CollectionVi...
- 標(biāo)題比較長,項(xiàng)目中遇到需要用tableivew和collectionview在一個頁面顯示,并且兩個控件里的內(nèi)容需...
- self.contentView.layer.masksToBounds = YES; self.contentV...
- 今天碰到cellForItemAtIndexPath這個數(shù)據(jù)源方法沒有被調(diào)用员魏。百度了一下解決方法丑蛤,大致有以下幾種:...