class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
//1. 系统默认提供布局方式: 多行多列布局
let layout = UICollectionViewFlowLayout()
// //2. 设置Cell的大小
layout.itemSize = CGSize(width: self.view.bounds.size.width, height: 200)
// //3. 滚动方向
layout.scrollDirection = .Horizontal
//设置上下左右的间距
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
//获取内容的偏移量
let offset = collectionView.contentOffset
//根据位置获取Cell
let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x, y: 0))
if indexPath!.item == 4 {
//滚动某个Cell到可见区域
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)
}
}
//设置Section的个数
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
//Section中Cell的个数
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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题: 编译执行UICollectionView cellForItemAtIndexPath的 UICollec...
- 在UITableView的Cell里嵌套使用CollectionView场景里,如果在点击CollectionVi...
- 标题比较长,项目中遇到需要用tableivew和collectionview在一个页面显示,并且两个控件里的内容需...
- self.contentView.layer.masksToBounds = YES; self.contentV...
- 今天碰到cellForItemAtIndexPath这个数据源方法没有被调用。百度了一下解决方法,大致有以下几种:...