1、UITableView掌握
? ? ? 1> ?設置UITableView的dataSource、delegate?
? ? ??2>?? ?UITableView多組數據和單組數據的展示?
? ? ? 3> ?UITableViewCell的常見屬性?
? ? ??4>? ? UITableView的性能優化(cell的循環利用)
? ? ? 5> ?自定義Cell
2、什么是UITableView
? ??在iOS中,要實現展示列表數據,最常用的做法就是使用UITableView。UITableView繼承自UIScrollView,因此支持垂直滾動,而且性能極佳
3、如何展示數據??
-
?UITableView需要一個數據源(dataSource)來顯示數據
-
UITableView會向數據源查詢一共有多少行數據以及每一行顯示什么數據等
-
沒有設置數據源的UITableView只是個空殼
-
凡是遵守UITableViewDataSource協議的OC對象,都可以是UITableView的數據源
4、UITableViewCell
4.1 ?UITableViewCell簡介:
?UITableView的每一行都是一個UITableViewCell,通過dataSource的tableView:cellForRowAtIndexPath:方法來初始化每一行
?UITableViewCell內部有個默認的子視圖:contentView,contentView是UITableViewCell所顯示內容的父視圖,可顯示一些輔助指示視圖
4.2 UITableViewCell的contentView
? ?contentView下默認有3個子視圖

4.3 UITableViewCell結構
4.4?UITableViewCell的重用原理
4.5?Cell的重用代碼
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 // 1.定義一個cell的標識 4 static NSString *ID = @”czcell"; 5 6 // 2.從緩存池中取出cell 7 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 8 9 // 3.如果緩存池中沒有cell 10 if (cell == nil) { 11 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 12 }
5、UITableView、UITableViewController、代理與數據源之間的關系,如下圖所屬:
6、UITableView和數據源
?
1.?tableView展示數據
? ? //?1.調用數據源的下面方法得知一共有多少組數據
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
??// 2.調用數據源的下面方法得知每一組有多少行數據
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
? ?// 3.調用數據源的下面方法得知每一行顯示什么內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
7、UITableView的常見屬性
? ?1. 常用屬性
? ??//1. 修改tableView的行高
self.tableView.rowHeight = 100;
?// 2.組頭組尾的高
self.tableView.sectionHeaderHeight = 55;self.tableView.sectionFooterHeight = 22;
?// 3.設置整個tablView的頭部/尾部視圖
self.tableView.tableHeaderView = [[UISwitch alloc] init];self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark];
?// 4.設置我們分割線顏色(clearColor相當于取消系統分割線)
self.tableView.separatorColor = [UIColor clearColor];
?// 5.設置分割線樣式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 設置索引條內部文字顏色
self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
// 設置索引條背景顏色
self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
// 允許UITableView多選
self.tableView.allowsMultipleSelection = YES;
?
?
// 獲取選中多行
NSArray *array = self.tableView.indexPathsForSelectedRows;
?// 獲取選中單行
NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
?// 讓UITableView進入編輯狀態,會出現左滑效果(結合代理方法commitEditingStyle...處理左滑效果時編輯事件處理)
self.tableView.editing = YES;
? ?2、常見方法(數據源與代理方法)
#pragma mark - 數據源方法
// 返回行數
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
// 設置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
。。。。。。。。。。。
}#pragma mark - 代理方法
/**
* 設置行高
*/
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{return 100;
}// 添加每組的組頭
- (UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
}// 返回每組的組尾
- (UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section{}// 選中某行cell時會調用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{NSLog(@"選中didSelectRowAtIndexPath row = %ld", indexPath.row);
}/*
2015-07-21 10:01:56.261 02-UITableView單組數據展示[1305:36752] 選中didSelectRowAtIndexPath row = 0
2015-07-21 10:01:58.212 02-UITableView單組數據展示[1305:36752] 取消選中 didDeselectRowAtIndexPath row = 0
2015-07-21 10:01:58.212 02-UITableView單組數據展示[1305:36752] 選中didSelectRowAtIndexPath row = 1
*/
// 取消選中某行cell會調用 (當我選中第0行的時候,如果現在要改為選中第1行 - 》會先取消選中第0行,然后調用選中第1行的操作)
- (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{NSLog(@"取消選中 didDeselectRowAtIndexPath row = %ld ", indexPath.row);
}// 設置UITableView的索引條方法:
// 設置UITableView的索引條,返回數組字符串集合
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;{NSArray *carGroupModels = self.carGroups;// 利用KVC獲取指定屬性的集合NSArray *array = [carGroupModels valueForKeyPath:@"title"];return array;
}
索引條顏色與背景設置:
// 設置索引條內部文字顏色self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];// 設置索引條背景顏色self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
3、數據刷新
注意:
? * 添加刷新:使用UITableView的 insertRowsAtIndexPaths...方法
? * 修改刷新:使用UITableView的 reloadRowsAtIndexPaths...方法(該方法使用的前提是模型數據的個數不變,所以添加與刪除不能采用此方法進行UITableView刷新功能。)
? * 刪除刷新:使用UITableView的 deleteRowsAtIndexPaths...方法
謝謝自己的堅持?,而沒有懈怠,今天就到處結束,明天我講系統的梳理自定義等高與不等高UITableViewCell,分別用frame、xib、storyboard、Autolayout實現,也將在其中使用第三方框架:Masonry與MJExtension,以便大家互相學習。睡覺了,晚安,^_^