UITablView大总结

作为iOS开发中最常用的控件,最基本的控件,同时也是很复杂的控件,其构成了APP的基本实现,最为一名老司机,我觉得很有必要对其做个传记.

首先,介绍一下写这篇文章的原因.

  • 是否你还在用frame来初始化cell,那你的行高当然还得每一个都得计算.
  • 是否不知道怎么样来设置其分割线顶到头,而不是系统的样式
  • 是否设置为group时 发现每一个section的header的高度 和 footer的高度 会有一个默认的高度
  • 点击cell时 其子视图的背景色会受影响 怎么办
  • 在iOS 10 iOS 9 iOS 8 iOS7中都新添加了什么方法 和 属性.
  • tableview的性能优化,你都知道哪些方法

我想这些问题,如果你都能说出几个答案,那么恭喜你,你是合格的老司机!!!
那如果你不知道,请系好安全带,老司机要发车了.

研究一下其API

常用的方法为

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
var rootTableView = UITableView.init(frame: CGRect(x:0,y:64,width:kSCREENWIDTH,height:kSCREENHEIGHT - 64), style: .plain)
rootTableView.xlpInitTableViewEnd(delegate: self, superView: self.view, cellClass: [commentCell.self])
func xlpInitTableViewEnd(delegate:Any,superView:UIView,cellClass:[AnyClass?]?) {
self.backgroundColor = .clear
self.delegate = delegate as? UITableViewDelegate
self.dataSource = delegate as? UITableViewDataSource
self.separatorStyle = UITableViewCellSeparatorStyle.singleLine
if cellClass != nil {
for cell in cellClass! {
let identifier:String = NSStringFromClass(cell!) as String
var lastStr :String = ""
if identifier.components(separatedBy: ".").count > 0 {
lastStr = identifier.components(separatedBy: ".").last!
}
self.register(cell, forCellReuseIdentifier: lastStr)
}
}
superView.addSubview(self)
}
//其相关的代理方法
numberofRows
numberOfSection
cellForRow
heightForRow
viewForHeader
viewForFooter

以上这些方法在ios7及以后的版本基本满足一般的需求.

那么现在是否有哪些好的方法或者改进呢,那当然我们要看看是否有新添加的属性或方法.

新增属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//ios7
estimatedRowHeight
estimatedSectionHeaderHeight
estimatedSectionFooterHeight
separatorInset
sectionIndexBackgroundColor
//ios8
var separatorEffect: UIVisualEffect?
UITableViewRowActionStyle //
//ios9
var cellLayoutMarginsFollowReadableWidth: Bool
var remembersLastFocusedIndexPath: Bool
UITableViewFocusUpdateContext//
//ios10
var prefetchDataSource: UITableViewDataSourcePrefetching?

新增代理方法

首先我们了解一下 其UITableviewDelegate方法和UITableViewDatasource方法

1 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
heightForRowAt
heightForHeaderInSection
heightForFooterInSection
viewForHeaderInSection
viewForFooterInSection
accessoryButtonTappedForRowWith
didSelectRowAt
didDeselectRowAt
willSelectRowAt
willDeselectRowAt
shouldHighlightRowAt
didHighlightRowAt
didUnhighlightRowAt
willDisplay Cell
didEndDisplaying
willDisplayHeaderView
willDisplayFooterView
didEndDisplayingHeaderView
didEndDisplayingFooterView
//ios7
estimatedHeightForRowAt
estimatedHeightForHeaderInSection
estimatedHeightForFooterInSection
//编辑相关
editingStyleForRowAt
titleForDeleteConfirmationButtonForRowAt
editActionsForRowAt //8.0
shouldIndentWhileEditingRowAt //默认返回值为YES 只对grouped类型的tableview有用
willBeginEditingRowAt
didEndEditingRowAt
targetIndexPathForMoveFromRowAt
indentationLevelForRowAt
//粘贴复制 以下3个方法必须同时调用
shouldShowMenuForRowAt
canPerformAction
performAction
//Focus 焦点 这个是新的代理方法 ios9之后
canFocusRowAt
shouldUpdateFocusIn
didUpdateFocusIn
indexPathForPreferredFocusedView

2 UITableViewDatasource方法

1
2
3
4
5
6
7
8
9
10
11
numberOfRowsInSection
numberOfSections
cellForRowAt
titleForHeaderInSection
titleForFooterInSection
canEditRowAt indexPath
canMoveRowAt
sectionIndexTitles(for tableView: UITableView) -> [String]?
sectionForSectionIndexTitle title: String, at index: Int) -> Int
commit editingStyle
moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath

现在我们对于开篇的问题逐一解答.

问题1 是否你还在用frame来初始化cell,那你的行高当然还得每一个都得计算.

答案:对于初级开发者,这是最常用的方法,但带来的问题也很显然,frame是写死的,则横竖屏切换会出现问题,当然一般只支持竖屏的话,也可以忽略,但是尺寸总要适配,5s 6 6P,这样还要做设备机型判断.
使用这个方法来布局cell的话,则heightForRow,则必须要执行.在展示tableview之前,必须要把所有的cell的高度计算出来.往往涉及到字符串高度的计算.

ios7苹果推出AutoLayout,Masonary的出现

坚持原创技术分享,您的支持将鼓励我继续创作!