UITableView知識梳理須知—(一)

1、UITableView掌握

? ? ? 1> ?設置UITableView的dataSource、delegate?

? ? ??2>?? ?UITableView多組數據和單組數據的展示?

? ? ? 3> ?UITableViewCell的常見屬性?

? ? ??4>? ? UITableView的性能優化(cell的循環利用)

? ? ? 5> ?自定義Cell

2、什么是UITableView

? ??在iOS中,要實現展示列表數據,最常用的做法就是使用UITableView。UITableView繼承自UIScrollView,因此支持垂直滾動,而且性能極佳

3、如何展示數據??

  1. ?UITableView需要一個數據源(dataSource)來顯示數據

  2. UITableView會向數據源查詢一共有多少行數據以及每一行顯示什么數據等

  3. 沒有設置數據源的UITableView只是個空殼

  4. 凡是遵守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的優先級高于屬性accessoryType

4.2 UITableViewCell的contentView

? ?contentView下默認有3個子視圖

1、其中2個是UILabel(通過UITableViewCell的textLabeldetailTextLabel屬性訪問)
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; 

?// 獲取選中單行

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,以便大家互相學習。睡覺了,晚安,^_^

轉載于:https://www.cnblogs.com/cjpBlog/p/4669126.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375709.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375709.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375709.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Yarn中的幾種狀態機

1 概述 為了增大并發性&#xff0c;Yarn采用事件驅動的并發模型&#xff0c;將各種處理邏輯抽象成事件和調度器&#xff0c;將事件的處理過程用狀態機表示。什么是狀態機&#xff1f; 如果一個對象&#xff0c;其構成為若干個狀態&#xff0c;以及觸發這些狀態發生相互轉移的事…

反轉字符串里的單詞

4、反轉字符串里的單詞 給定一個字符串&#xff0c;逐個反轉字符串中的單詞 示例1&#xff1a; 輸入: "the sky is blue", 輸出: "blue is sky the".說明&#xff1a; 無空格字符構成一個單詞。 輸入字符串可以在前面或者后面包含多余的空格&#xff0…

正整數

題目鏈接&#xff1a;http://acm.hust.edu.cn/vjudge/contest/view.action?cid84077#problem/A 題目&#xff1a; Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the s…

360 webscan中防注入跨站攻擊的核心

//get攔截規則 $getfilter "\\<.javascript:window\\[.{1}\\\\x|<.*(&#\\d?;?)?>|<.*(data|src)data:text\\/html.*>|\\b(alert\\(|confirm\\(|expression\\(|prompt\\(|benchmark\s*?\\(\d?|sleep\s*?\\([\d\.]?\\)|load_file\s*?\\()|<[…

POJ 2115 C Looooops(擴展歐幾里得)

輾轉相除法&#xff08;歐幾里得算法&#xff09; 時間復雜度&#xff1a;在O(logmax(a, b))以內 int gcd(int a, int b) {if (b 0) return a;return gcd(b, a % b); }擴展歐幾里得算法 時間復雜度和歐幾里得算法相同 int extgcd(int a, int b, int& x, int& y) {int …

分支管理(轉載)

轉自&#xff1a;http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743862006503a1c5bf5a783434581661a3cc2084efa000 分支就是科幻電影里面的平行宇宙&#xff0c;當你正在電腦前努力學習Git的時候&#xff0c;另一個你正在另一個平行…

匹配括號

輸入&#xff1a; 僅包含{,},(,),[,]的字符串輸出&#xff1a; 如果括號匹配輸出&#xff1a;YES 否則輸出&#xff1a;NOSolution&#xff1a; #include<iostream> #include<string> #include<stack> using namespace std;bool check(const string&)…

總線接口與計算機通信

微機中總線一般有內部總線、系統總線和外部總線。 內部總線是微機內部各外圍芯片與處理器之間的總線&#xff0c;用于芯片一級的互連&#xff1b; 系統總線是微機中各插件板與系統板之間的總線&#xff0c;用于插件板一級的互連&#xff1b; 外部總線則是微機和外部設備之間的總…

uva 12442 . Forwarding Emails

“... so forward this to ten other people, to prove that you believe the emperor has new clothes.”Aren’t those sorts of emails annoying?Martians get those sorts of emails too, but they have an innovative way of dealing with them.Instead of just forwardi…

大數相加

輸入&#xff1a; 兩個用字符串表示的大整數 如a1111111111111,b222222222222222 輸出&#xff1a; 兩個數的和 Solution&#xff1a; #include<iostream> #include<algorithm> #include<string>using namespace std;int add(const char&,const char&…

Linux的進程與服務(一)

啟動的配置文件/etc/inittab&#xff0c;修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not h…

Linux 修改swap虛擬內存大小

swap是內存的交換區&#xff1b;換句話說&#xff0c;如果內存不夠用了&#xff0c;那么系統會在硬盤上存儲一些內存中不常用的數據&#xff0c;之后將這部分數據在存儲中析構掉&#xff1b;這樣內存就又有剩余空間可以運行東東啦&#xff0c;這個過程也就是所謂的交換&#xf…

統計文章中的單詞

輸入&#xff1a; 字符串&#xff0c;其中可能包含空格&#xff0c;TAB&#xff0c;回車等&#xff0c;規定&#xff0c;僅字母數字和單引號算作單詞部分 輸出&#xff1a; 單詞的個數 Solution&#xff1a; #include<iostream> #include<string>using namespac…

邁向世界 拓展未來

一切都會過去&#xff0c;只有真理永存&#xff0c;只有愿意越過事實前進一步的人&#xff0c;才能理解事實&#xff0c;這就是科學。時代在發展&#xff0c;科技更是日新月異徹底改變著我們的生活方式。現在的我們就是跟著科技發展的腳步&#xff0c;奔著夢想&#xff0c;一直…

JS - 跳轉頁面

<!-- 第一種&#xff1a; --><script type"text/javascript">window.location.href "login.jsp?backurl" window.location.href;</script><!-- 第二種&#xff1a; --><script type"text/javascript"&g…

分享一個用安卓手機就能引導pc安裝linux系統辦法

1、首先安卓手機下載軟件DriveDroid.apk http://pan.baidu.com/s/1qW4pbT6 2、下載linux鏡像文件放手機存儲卡存儲&#xff0c;放到Download/images/以下 3、打開軟件會自己主動讀取這個目錄以下鏡像&#xff0c;也能夠在軟件里面下載須要的鏡像文件 4、軟件設置usb連接模式 5、…

SharePoint 2013 開發——其他社交功能

博客地址&#xff1a;http://blog.csdn.net/FoxDave上一篇講了如何獲取用戶配置文件的相關屬性&#xff0c;它屬于SharePoint 2013社交功能的一個小的構成部分。社交功能是SharePoint 2013改進的一大亮點。可以在現有網站上開啟社交功能或者新建一個專門用于社交用途的社區網站…

第一個Qt+opencv程序

簡單安裝好Qt和編譯安裝好opencv后&#xff0c;簡單實現第一個Qtopencv程序&#xff1a;讀取并顯示一張圖片&#xff0c;這里我的Qt版本時5.9.1&#xff0c;opencv版本是4.0.1&#xff0c;版本的影響不大。 首先我們用Qt創建一個控制臺項目&#xff0c;即在創建項目時選擇Qt C…

redis學習筆記——應用場景

最近在看redis入門指南&#xff0c;現在就自己的學習情況說說自己的理解。 字符串類型&#xff08;String&#xff09; 字符串類型是Redis中最基本的類型&#xff0c;能存儲任意形式的字符串&#xff0c;包括二進制數據。如一張照片也可以用字符串類型存儲。注意字符串類型鍵允…

Unity的Cover flow的實現包(2個)

蘋果的mac機上預覽圖片&#xff0c;有一個所謂的cover flow的效果&#xff0c;這里收集到兩個&#xff0c;兩個實現效果略有不同。 1、老外的實現 https://github.com/rakkarage/Unity3D-CoverFlow 這個焦點圖片在到最后位置前會模擬一個抖動效果 2、國人的實現 http://game.ce…