国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發之UITableView詳解

2019-10-21 18:54:50
字體:
來源:轉載
供稿:網友
在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似于微信、QQ、新浪微博等軟件基本上隨處都是UITableView。當然它的廣泛使用自然離不開它強大的功能,今天這篇文章將針對UITableView重點展開討論
 

一、UITableView基本介紹

默認的UITableView有2種風格:

  1. UITableViewStylePlain(不分組)
  2. UITableViewStyleGrouped(分組)

UITableView中的數據只有行的概念,沒有列的概念,UITableView的每行數據就是一個UITableViewCell。
自帶的UITableViewCell的類型選擇有:

 

復制代碼代碼如下:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // 左側顯示textLabel(不顯示detailTextLabel),imageView可選(顯示在最左邊)
    UITableViewCellStyleValue1,     // 左側顯示textLabel、右側顯示detailTextLabel(默認藍色),imageView可選(顯示在最左邊)
    UITableViewCellStyleValue2,     // 左側依次顯示textLabel(默認藍色)和detailTextLabel,imageView可選(顯示在最左邊)
    UITableViewCellStyleSubtitle    // 左上方顯示textLabel,左下方顯示detailTextLabel(默認灰色),imageView可選(顯示在最左邊)
};

 

二、UITableViewDataSource數據源

數據源的作用就是告訴UITableView,我該顯示什么數據

 

復制代碼代碼如下:

#pragma mark 常用數據源方法
#pragma mark 返回分組數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
#pragma mark 返回每組行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
#pragma mark 返回每行的單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 返回每組頭標題名稱
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
#pragma mark 返回每組尾部說明
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

 

計算分組數 -> 計算每組行數 -> 生成分組索引 -> 生成單元格
注意:cellForRowAtIndexPath只生產當前顯示在界面上的單元格

三、UITableViewDelegate代理

代理的作用是告訴UITableView,我該怎么顯示和響應

 

復制代碼代碼如下:

#pragma mark - 常用代理方法
#pragma mark 設置分組頭部的內容高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
#pragma mark 設置每行高度(每行高度可以不一樣)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 設置分組尾部的內容高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
#pragma mark 點擊了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 設置分組的頭部視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
#pragma mark 設置分組的尾部視圖
- (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 刪除時刷新指定的行數據
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 添加時刷新指定的行數據
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 

五、UITableViewCell的重用機制

在UITableView內部有一個緩存池,專門用來緩存UITableViewCell,因為UITableView不是一下子顯示全部Cell,而是以 所見即所得 的方式,手機上看的見的Cell,才有存在的對象UITableViewCell實例。具體表現如下:

每次顯示新的Cell的時候,都是先從緩存池中取出對應的UITableViewCell對象,進行 重新初始化 顯示。如果緩存池中沒有,才創建新的UITableViewCell對象
每當某個Cell被移出 可見區域 外后,就會被 回收 到緩存池中
所以盡管要展示的數據巨大,但內存中存在的UITableViewCell也是有限的,極大的降低了對內存的需求。

 

復制代碼代碼如下:

# pragma mark 在tableView:cellForRowAtIndexPath:方法中使用UITableView的重用機制
// 由于此方法調用十分頻繁,cell的標示聲明成靜態變量有利于性能優化
static NSString *cellIdentifier = @"UITableViewCellIdentifierKey1";
// 首先根據標識去緩存池取
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 如果緩存池沒有找到,則重新創建并放到緩存池中
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}

 

六、系統自帶的UITableViewCell

iOS開發,UITableView

我們基本上很少使用系統自帶的UITableViewCell,樣式太過于死板了。

七、自定義Cell

基本步驟:
自定義類XXXTableViewCell,繼承UITableViewCell
重寫-(id)initWithStyle:reuseIdentifier:方法,添加子控件
最好重寫layoutSubView方法,設置子控件frame
然后在UITableView的代理方法tableView:cellForRowAtIndexPath:中使用重用機制創建該類XXXTableViewCell,再對cell進行初始化

iOS開發,UITableView

八、MVC模式

iOS開發,UITableView  



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚宁县| 剑河县| 海淀区| 黄冈市| 共和县| 疏附县| 当雄县| 土默特右旗| 鹿泉市| 唐河县| 洛南县| 昌平区| 景洪市| 囊谦县| 察雅县| 谢通门县| 濉溪县| 安阳县| 孝昌县| 奉新县| 肥西县| 云龙县| 饶河县| 叶城县| 闸北区| 清新县| 敖汉旗| 广州市| 库尔勒市| 响水县| 盐亭县| 房产| 富锦市| 蒙城县| 乐平市| 分宜县| 扎囊县| 白山市| 吐鲁番市| 新宁县| 新竹市|