UITableView知识梳理须知—(一)
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所顯示內容的父視圖,可顯示一些輔助指示視圖
輔助指示視圖的作用是顯示一個表示動作的圖標,可以通過設置UITableViewCell的accessoryType來顯示,默認是UITableViewCellAccessoryNone(不顯示輔助指示視圖),其他值如下: UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailButton UITableViewCellAccessoryDetailDisclosureButton UITableViewCellAccessoryCheckmark 還可以通過cell的accessoryView屬性來自定義輔助指示視圖(比如往右邊放一個開關),cell的屬性accessoryView的優先級高于屬性accessoryType4.2 UITableViewCell的contentView
? ?contentView下默認有3個子視圖
1、其中2個是UILabel(通過UITableViewCell的textLabel和detailTextLabel屬性訪問) 2、第3個是UIImageView(通過UITableViewCell的imageView屬性訪問) 3、UITableViewCell還有一個UITableViewCellStyle屬性,用于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置? ? ?4.3 UITableViewCell結構
4.4?UITableViewCell的重用原理
iOS設備的內存有限,如果用UITableView顯示成千上萬條數據,就需要成千上萬個UITableViewCell對象的話,那將會耗盡iOS設備的內存。要解決該問題,需要重用UITableViewCell對象? ? ? ? ? ?Cell重用原理:當滾動列表時,部分UITableViewCell會移出窗口,UITableView會將窗口外的UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource返回UITableViewCell時,dataSource會先查看這個對象池,如果池中有未使用的UITableViewCell,dataSource會用新的數據配置這個UITableViewCell,然后返回給UITableView,重新顯示到窗口中,從而避免創建新對象 ? ?? 還有一個非常重要的問題:有時候需要自定義UITableViewCell(用一個子類繼承UITableViewCell),而且每一行用的不一定是同一種UITableViewCell,所以一個UITableView可能擁有不同類型的UITableViewCell,對象池中也會有很多不同類型的UITableViewCell,那么UITableView在重用UITableViewCell時可能會得到錯誤類型的UITableViewCell 解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置reuseIdentifier(一般用UITableViewCell的類名)。當UITableView要求dataSource返回UITableViewCell時,先通過一個字符串標識到對象池中查找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳入這個字符串標識來初始化一個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;?// 獲取選中單行
?// 讓UITableView進入編輯狀態,會出現左滑效果(結合代理方法commitEditingStyle...處理左滑效果時編輯事件處理)
? ?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,以便大家互相學習。睡覺了,晚安,^_^
轉載于:https://www.cnblogs.com/cjpBlog/p/4669126.html
總結
以上是生活随笔為你收集整理的UITableView知识梳理须知—(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今天的一点点收获
- 下一篇: POJ 2115 C Looooops(