iOS開發(fā)之UITableView詳解
一、UITableView基本介紹
默認(rèn)的UITableView有2種風(fēng)格:
- UITableViewStylePlain(不分組)
- UITableViewStyleGrouped(分組)
UITableView中的數(shù)據(jù)只有行的概念,沒有列的概念,UITableView的每行數(shù)據(jù)就是一個UITableViewCell。
自帶的UITableViewCell的類型選擇有:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 左側(cè)顯示textLabel(不顯示detailTextLabel),imageView可選(顯示在最左邊)
UITableViewCellStyleValue1, // 左側(cè)顯示textLabel、右側(cè)顯示detailTextLabel(默認(rèn)藍(lán)色),imageView可選(顯示在最左邊)
UITableViewCellStyleValue2, // 左側(cè)依次顯示textLabel(默認(rèn)藍(lán)色)和detailTextLabel,imageView可選(顯示在最左邊)
UITableViewCellStyleSubtitle // 左上方顯示textLabel,左下方顯示detailTextLabel(默認(rèn)灰色),imageView可選(顯示在最左邊)
};
二、UITableViewDataSource數(shù)據(jù)源
數(shù)據(jù)源的作用就是告訴UITableView,我該顯示什么數(shù)據(jù)
#pragma mark 常用數(shù)據(jù)源方法
#pragma mark 返回分組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
#pragma mark 返回每組行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
#pragma mark 返回每行的單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 返回每組頭標(biāo)題名稱
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
#pragma mark 返回每組尾部說明
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
計(jì)算分組數(shù) -> 計(jì)算每組行數(shù) -> 生成分組索引 -> 生成單元格
注意:cellForRowAtIndexPath只生產(chǎn)當(dāng)前顯示在界面上的單元格
三、UITableViewDelegate代理
代理的作用是告訴UITableView,我該怎么顯示和響應(yīng)
#pragma mark - 常用代理方法
#pragma mark 設(shè)置分組頭部的內(nèi)容高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
#pragma mark 設(shè)置每行高度(每行高度可以不一樣)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 設(shè)置分組尾部的內(nèi)容高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
#pragma mark 點(diǎn)擊了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 設(shè)置分組的頭部視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
#pragma mark 設(shè)置分組的尾部視圖
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
四、UITableView刷新列表方法
#pragma mark 刷新整個表格
- (void)reloadData;
#pragma mark 刷新指定的行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 刷新指定的分組
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 刪除時刷新指定的行數(shù)據(jù)
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 添加時刷新指定的行數(shù)據(jù)
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
五、UITableViewCell的重用機(jī)制
在UITableView內(nèi)部有一個緩存池,專門用來緩存UITableViewCell,因?yàn)閁ITableView不是一下子顯示全部Cell,而是以 所見即所得 的方式,手機(jī)上看的見的Cell,才有存在的對象UITableViewCell實(shí)例。具體表現(xiàn)如下:
每次顯示新的Cell的時候,都是先從緩存池中取出對應(yīng)的UITableViewCell對象,進(jìn)行 重新初始化 顯示。如果緩存池中沒有,才創(chuàng)建新的UITableViewCell對象
每當(dāng)某個Cell被移出 可見區(qū)域 外后,就會被 回收 到緩存池中
所以盡管要展示的數(shù)據(jù)巨大,但內(nèi)存中存在的UITableViewCell也是有限的,極大的降低了對內(nèi)存的需求。
# pragma mark 在tableView:cellForRowAtIndexPath:方法中使用UITableView的重用機(jī)制
// 由于此方法調(diào)用十分頻繁,cell的標(biāo)示聲明成靜態(tài)變量有利于性能優(yōu)化
static NSString *cellIdentifier = @"UITableViewCellIdentifierKey1";
// 首先根據(jù)標(biāo)識去緩存池取
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 如果緩存池沒有找到,則重新創(chuàng)建并放到緩存池中
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
六、系統(tǒng)自帶的UITableViewCell
我們基本上很少使用系統(tǒng)自帶的UITableViewCell,樣式太過于死板了。
七、自定義Cell
基本步驟:
自定義類XXXTableViewCell,繼承UITableViewCell
重寫-(id)initWithStyle:reuseIdentifier:方法,添加子控件
最好重寫layoutSubView方法,設(shè)置子控件frame
然后在UITableView的代理方法tableView:cellForRowAtIndexPath:中使用重用機(jī)制創(chuàng)建該類XXXTableViewCell,再對cell進(jìn)行初始化
八、MVC模式
- iOS開發(fā)之UITableView與UISearchController實(shí)現(xiàn)搜索及上拉加載,下拉刷新實(shí)例代碼
- iOS App開發(fā)中使用及自定義UITableViewCell的教程
- 詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法
- iOS程序開發(fā)中設(shè)置UITableView的全屏分隔線的方法(不畫線)
- 全面解析iOS應(yīng)用中自定義UITableViewCell的方法
- 改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法
- iOS應(yīng)用開發(fā)中UITableView的分割線的一些設(shè)置技巧
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- iOS應(yīng)用中UITableView左滑自定義選項(xiàng)及批量刪除的實(shí)現(xiàn)
- ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決辦法(去掉15px空白間距)
- 詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)
- iOS開發(fā)中UITableview控件的基本使用及性能優(yōu)化方法
- 詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新
相關(guān)文章
IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實(shí)例詳解
這篇文章主要介紹了IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04詳解ios監(jiān)聽reloadData刷新列表完畢的時機(jī)
這篇文章主要介紹了詳解ios監(jiān)聽reloadData刷新列表完畢的時機(jī),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié)
除了使用SQL關(guān)系型數(shù)據(jù)庫,我們還可以使用Xcode中提供的Core Data來進(jìn)行表結(jié)構(gòu)數(shù)據(jù)處理,這里我們就來初步整理iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié):2016-06-06iOS UILabel根據(jù)內(nèi)容自動調(diào)整高度
這篇文章主要為大家詳細(xì)介紹了iOS UILabel根據(jù)內(nèi)容自動調(diào)整高度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能
這篇文章主要為大家詳細(xì)介紹了iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法
下面小編就為大家分享一篇iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12