1.UICollectionView的常用属性
init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
var collectionViewLayout: UICollectionViewLayout
unowned(unsafe) var delegate: UICollectionViewDelegate?
unowned(unsafe) var dataSource: UICollectionViewDataSource?
var backgroundView: UIView?
var allowsSelection: Bool
var allowsMultipleSelection: Bool
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
UICollectionViewCell显示的样式
struct UICollectionViewScrollPosition : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)
static var None: UICollectionViewScrollPosition { get }
static var CenteredVertically: UICollectionViewScrollPosition { get }
static var Bottom: UICollectionViewScrollPosition { get }
static var Left: UICollectionViewScrollPosition { get }
static var CenteredHorizontally: UICollectionViewScrollPosition { get }
static var Right: UICollectionViewScrollPosition { get }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
2.UICollectionView常用的方法
func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
func registerClass(viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
func registerNib(nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
func dequeueReusableCellWithReuseIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath!) -> AnyObject
func dequeueReusableSupplementaryViewOfKind(elementKind: String, withReuseIdentifier identifier: String, forIndexPath indexPath: NSIndexPath!) -> AnyObject
func indexPathsForSelectedItems() -> [AnyObject]
func selectItemAtIndexPath(indexPath: NSIndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition)
func deselectItemAtIndexPath(indexPath: NSIndexPath?, animated: Bool)
func reloadData()
func setCollectionViewLayout(layout: UICollectionViewLayout, animated: Bool)
func setCollectionViewLayout(layout: UICollectionViewLayout, animated: Bool, completion: ((Bool) -> Void)!)
func numberOfSections() -> Int
func numberOfItemsInSection(section: Int) -> Int
func scrollToItemAtIndexPath(indexPath: NSIndexPath, atScrollPosition scrollPosition: UICollectionViewScrollPosition, animated: Bool)
func insertSections(sections: NSIndexSet)
func deleteSections(sections: NSIndexSet)
func reloadSections(sections: NSIndexSet)
func moveSection(section: Int, toSection newSection: Int)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
UICollectionView代理方法
optional func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
optional func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
UICollectionView数据源方法
// 1.设置UICollectionView有多少个Item
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
// 2.设置 UICollectionViewCell 所显示的内容, 以及索引路径
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
// 3.设置 UICollectionView 有多少组 Cell
optional func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
UICollectionView的集视图布局方法
// 1.该方法是用来设置 UICollectionView 的 Item 尺寸大小
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
// 2.该方法是用来设置 UICollectionView 的 Item 四周的边界
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
// 3.该方法是用来设置 UICollectionView 的 Item 上下之间的最小间距(如果在自定义UICollectionView中实现了该属性, 那么该方法就会覆盖掉原来的属性)
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
// 4.该方法是用来设置 UICollectionView 的 Item 左右之间的最小间距(如果在自定义UICollectionView中实现了该属性, 那么该方法就会覆盖掉原来的属性)
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
// 5.该方法是用来设置 UICollectionView 的页头尺寸(如果在自定义UICollectionView中实现了该属性, 那么该方法就会覆盖掉原来的属性)
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
// 6.该方法是用来设置 UIcollectionView 的页尾尺寸(如果在自定义UICollectionView中实现了该属性, 那么该方法就会覆盖掉原来的属性)
optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3.代码演示
首先我们要遵守以下协议
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
}
自定义UICollectionView
func myCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSizeMake(90, 120)
flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20)
flowLayout.minimumLineSpacing = 20
flowLayout.minimumInteritemSpacing = 20
flowLayout.headerReferenceSize = CGSizeMake(100, 50)
flowLayout.footerReferenceSize = CGSizeMake(100, 50)
var collection = UICollectionView(frame: CGRectMake(0, 64, self.view.frame.width, self.view.frame.height - 64), collectionViewLayout: flowLayout)
collection.backgroundColor = UIColor.whiteColor()
collection.alwaysBounceVertical = true
collection.alwaysBounceHorizontal = true
collection.dataSource = self
collection.delegate = self
collection.allowsSelection = true
collection.allowsMultipleSelection = false
collection.pagingEnabled = true
collection.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
self.view.addSubview(collection)
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
自定义UINavigationBar
func myNavigationBar() {
// 1.自定义 NavigationBar, 设置它的位置大小
var navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.width, 64))
// 2.设置 NavigationBar 的背景色
navigationBar.backgroundColor = UIColor.redColor()
// 3.自定义 NavigationItem 设定它的 Title
let navigationItem = UINavigationItem(title: "UICollectionView演示")
// 4.自定义 UIBarButtonItem 的Title, Style, Target 的对象, 已经监听的方法
let leftButton = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "back")
// 5.设置 Navigation 左边的按钮为 leftButton
navigationItem.leftBarButtonItem = leftButton
// 6.把 NavigationItem 添加到 NavigationBar
navigationBar.pushNavigationItem(navigationItem, animated: true)
// 7.添加到到 self.view 上
self.view.addSubview(navigationBar)
}
// 8.NavigationBar监听方法
func back() {
println("点击了返回")
}
UICollectionView的代理方法, 数据源方法, FlowLayout 方法
// 1.该方法是用来设置返回 CollectionViewCell 的组数
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
// 2.该方法是用来设置返回 CollectionViewCell 的个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
// 3.该方法是用来设置 CollectionViewCell 的内容
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var collectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
collectionCell.backgroundColor = UIColor.redColor()
return collectionCell
}
// 4.该方法是点击了 CollectionViewCell 时调用的监听方法
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("aaa")
}
// 5.该方法是用来设置 CollectionViewCell 的大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(90, 120)
}
// 6.该方法是用来设置 CollectionViewCell 四周的边距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(20, 20, 20, 20)
}
// 7.该方法是用来设置同一行 CollectionViewCell 之间的间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 20
}
// 8.该方法是用来设置同一列 CollectionViewCell 之间的间距
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 20
}
// 9.该方法是用来设置 CollectionView 的页头尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(100, 50)
}
// 10.该方法是用来设置 CollectionView 的页尾尺寸
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSizeMake(100, 50)
}