UIScrollView

1.UIScrollView

1
class UIScrollView : UIView
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
extension ScrollViewController: UIScrollViewDelegate {
//在拖动过程中会调用多次
func scrollViewDidScroll(_ scrollView: UIScrollView) { }

//缩放时调用,会调用多次
func scrollViewDidZoom(_ scrollView: UIScrollView) { } // any zoom scale changes

//拖动开始时调用
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { }

//手指抬起时调用,速度以点或者毫秒为单位
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { }

//手指抬起后调用,之后才会减速
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { }

//将要减速时调用
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { }

//减速结束之后调用
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { }

//拖动后的动画结束之后调用,没有动画不会调用
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { }

//返回缩放的视图
func viewForZooming(in scrollView: UIScrollView) -> UIView? { }

//开始缩放时调用
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { }

//缩放结束时调用
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) { }

//是否可以拖到顶部
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { }

//拖到顶部时调用
func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { }

//adjustedContentInset变化时调用
func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView) { }
}
//运行顺序
WillBeginDragging
WillEndDragging
DidEndDragging
WillBeginDecelerating
DidEndDecelerating

2.UITableView

1
class UITableView : UIScrollView
1
2
3
4
class VC: UIViewController {
let data: [Int] = []
let tableView = UITableView()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//MARK: - TableViewDataSource
extension VC: UITableViewDataSource {
//每个section中的row的个数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

//自定义每个cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier) as? TableViewCell else {
let newCell = TableViewCell(style: .default, reuseIdentifier: TableViewCell.identifier)
newCell.bindData(data: data[indexPath.row])
return newCell
}
cell.bindData(data: data[indexPath.row])
return cell
}
}
1
2
3
4
//MARK: - TableViewDelegate
extension VC: UITableViewDelegate {
//都是可选的
}
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
//MARK: - TableViewCell
class TableViewCell: UITableViewCell {
static let identifier = "TableViewCellIdentifier"
let label = UILabel()
let button = UIButton()
var data: Int = 0

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupSubviews()
setupLayout()
}

//设置子视图
func setupSubviews() {
}

//设置布局
func setupLayout() {
}

//从外界接收数据
func bindData(data: Int) {
self.data = data
}
}

3.UICollectionView

1
class UICollectionView : UIScrollView
1
2
3
4
5
//MARK: - CollectionViewController
class CollectionViewController: UIViewController {
let data: [Int] = []
let collectionView = UICollectionView()
}
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
//MARK: - UICollectionViewDataSource
extension CollectionViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell else {
let newCell = CollectionViewCell(frame: .zero)
newCell.bindData(data: data[indexPath.item])
return newCell
}
cell.bindData(data: data[indexPath.item])
return cell
}

//section的个数
func numberOfSections(in collectionView: UICollectionView) -> Int {}

//自定义kind对应的视图,要应用从dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:中获取的view
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {}

//每个item是否可移动
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {}

//把一个item移动到指定位置
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}

//在view中显示索引字母["A", "B", "C" ... "Z", "#"]
func indexTitles(for collectionView: UICollectionView) -> [String]? {}

//根据索引字母和序号(e.g. "B",1)返回indexpath
func collectionView(_ collectionView: UICollectionView, indexPathForIndexTitle title: String, at index: Int) -> IndexPath {}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//MARK: - UICollectionViewDelegate
extension CollectionViewController: UICollectionViewDelegateFlowLayout {
//每个item的大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 100)
}

//每个section的边距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { }

//每个item之间的行间距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { }

//每个item之间的间隔
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { }

//header的大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { }

//footer的大小
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
18
19
20
21
22
23
24
25
26
27
28
//MARK: - CollectionViewCell
class CollectionViewCell: UICollectionViewCell {
static let identifier = "CollectionViewCellIdentifier"
let label = UILabel()
let button = UIButton()
var data: Int = 0

override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
setupLayout()
}

//设置子视图
func setupSubviews() { }

//设置布局
func setupLayout() { }

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

//从外界接收数据
func bindData(data: Int) {
self.data = data
}
}

reloadData()之后的函数调用顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
numberOfSections2
numberOfItems3InSection0
numberOfItems2InSection1
sizeForItemAt[0, 0]
sizeForItemAt[0, 1]
sizeForItemAt[0, 2]
insetForSectionAt0
minimumLineSpacingForSectionAt0
referenceSizeForHeaderInSection0
sizeForItemAt[1, 0]
sizeForItemAt[1, 1]
insetForSectionAt1
minimumLineSpacingForSectionAt1
referenceSizeForHeaderInSection1
cellForItemAt[0, 0]
cellForItemAt[0, 1]
cellForItemAt[0, 2]
cellForItemAt[1, 0]
cellForItemAt[1, 1]
viewForSupplementaryElementOfKind:UICollectionElementKindSectionHeader
viewForSupplementaryElementOfKind:UICollectionElementKindSectionHeader
0%