表視圖的基本概念和用法

1、表視圖的基本概念

·UITableView的基本概念
·在iOS開發中,表視圖的應用十分廣泛與普遍。因此,熟練使用表視圖以及學習其原 理顯得至關重要。
·我們可以選擇創建表視圖也可以直接選擇創建表視圖控制器 
·UITableView基本樣式



·UITableView的風格
·表視圖存在兩種顯示風格,UITableViewStylePlain、UITableViewStyleGrouped



·表視圖的結構
·表視圖由頭部、尾部視圖,中間由一連串單元格視圖組成 
·表視圖的頭部視圖由tableHeaderView屬性設置,尾部視圖通過tableFooterView屬性設置
·分組表格由一連串section(默認是1)視圖組成,每一個section又包含一個連續 的單元格
·每一個section視圖又有頭部視圖和尾部視圖,通過委託方法設置

·創建一個簡單的表視圖

_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44) style:UITableViewStylePlain];
_tableView.delegate = self; // 設置tableView的委託
_tableView.dataSource = self; // 設置tableView的數據委託
[self.view addSubview:_tableView];
// 以下兩個數據源方法必須強制實現
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return 20;
} // 返回每個section中的單元格數,⼀一般以數組的形式表示section中有幾行[array count]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *identifier = @"myCell";
// 檢測、查詢是否有閒置的單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) {
          cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]
autorelease];
    }
return cell;
} // 返回每行的單元格對象

2、表視圖常用屬性和方法

·常用屬性

// 設置表視圖分割線風格
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; // 設置表視圖分割線顏色,默認標準灰色
@property(nonatomic,retain) UIColor *separatorColor;
// 設置表視圖的頭部視圖
@property(nonatomic,retain) UIView *tableHeaderView;
// 設置表視圖的尾部視圖
@property(nonatomic,retain) UIView *tableFooterView;
// 設置表視圖單元格的行高
@property(nonatomic) CGFloat rowHeight;
// 設置表視圖section頭部行高
@property(nonatomic) CGFloat sectionHeaderHeight;
// 設置表視圖section頭部行高
@property(nonatomic) CGFloat sectionFooterHeight;
// 設置表視圖背景
@property(nonatomic, readwrite, retain) UIView *backgroundView
// 刷新表視圖單元格中數據
- (void)reloadData;
// 刷新表視圖section中數據
- (void)reloadSectionIndexTitles
// 默認爲NO,不可以編輯,設置時,不存在動畫效果 
@property(nonatomic,getter=isEditing) BOOL editing;
// 覆蓋此方法,存在動畫效果
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
// 默認爲YES,當表視圖不在編輯時,單元格是否可以選中
@property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
// 默認爲NO,當表視圖在編輯時,單元格是否可以選中
@property(nonatomic) BOOL allowsSelectionDuringEditing;
// 默認爲NO,是否可以同時選中多個單元格,注意版本問題
@property(nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); 
// 默認爲NO,在編輯狀態下時,是否可以同時選中多個單元格,注意版本問題 @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);

·常用方法

//  指定⼀一個cell,返回⼀一個NSIndexPath實例,如果cell沒有顯示,返回nil
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
// 指定⼀一個範圍,返回⼀一個數組,內容是NSIndexPath實例,指定rect無效,返回nil
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
// 指定⼀一個NSIndexPath,返回⼀一個cell實例,如果cell沒有顯示,返回爲nil
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 根據顯示的cell,返回⼀一組cell實例的數組,如果沒有顯示,返回nil
- (NSArray *)visibleCells;
// 根據顯示的cell,返回⼀一組NSIndexPath實例的數組,如果沒有顯示,返回nil
- (NSArray *)indexPathsForVisibleRows;
// 滑動到指定的位置,可以配置動畫
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
//  插入⼀一行cell,指定⼀一個實現動畫效果
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
// 刪除⼀一行cell, 指定⼀一個實現動畫效果
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
// 刷新⼀一個行cell,指定⼀一個實現動畫效果
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
// 移動cell的位置,指定⼀一個實現動畫效果
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);

索引路徑
·NSIndexPath類
·它表示是一個路徑,在嵌套數組 集合中的特定的結點路徑。簡而言 之,可以通過它獲得到當前表視圖 其中的一行
·我們可以通過類方法創建一個 NSIndexPath實例,來指定特定行


·常用屬性和方法

//  指定特定的行和列
+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
@property(nonatomic,readonly) NSInteger section; // 指定分區 
@property(nonatomic,readonly) NSInteger row; // 指定行

3、數據源方法和委託方法

·數據源方法和委託方法
·表視圖的繼承自UIScrollView,這樣的繼承關係使得表視圖可以實現上、下滾動, 它的父類,我們將在後面的課程中再次提及
·數據源方法(UITableViewDatasource): 實例化表視圖時,必須要實現它的數據 源方法,以此來完成表中數據的配置(一般來說數據源方法是用來配置表中的數據)
·委託方法(UITableViewDelegate): 表視圖的委託方法(代理方法),一般是處 理表視圖基本樣式(單元格的高度)以及捕捉選中單元格選中事件等


表示視圖調用順序—委託方法

·創建和配置表視圖的順序
·創建表視圖實例,初始化風 格和大小
·設置數據源方法和委託方法 
·開始調用數據源方法,(注意事件循環沒有結束)

·調用順序如下圖所示:



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 1,默認爲1 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 3

常用數據源方法和委託方法

·常用數據源方法

//  配置section中含有行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
// 創建單元格實例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
// 配置表視圖section個數,默認爲1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// section中的頭部視圖的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section;
// section中的尾部視圖的標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection: (NSInteger)section;

數據源方法

·常用數據源方法

/* 表視圖的編輯 移動、刪除等 */
// 指定單元格是否支持編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 指定單元格是否支持移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 用戶編輯了哪⼀一個單元格,在這裏執行刪除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 實現此方法,移動單元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

常用委託方法

·常用委託方法

// 配置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath;
// 設置section 頭部、尾部視圖的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section;
// 自定義section頭部、尾部視圖,注意:需要指定高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section;
// 用戶單擊單元格中輔助按鈕時,調用該方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
// 用戶單擊單元格,調用該方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath;
// 取消單元格時,調用該方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// 設置單元格編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

4、單元格的基本用法

·單元格的重用
·考慮這樣的一種問題,假設表視圖中有上百個聯繫人(甚至更多),那麼我們需要 創建成百乃至上千個單元格對象嗎?答案是否定的!

static NSString *identifier = @"myCell"; // 靜態標識符
// 檢測查詢是否有閒置的單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
      if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
}
// 設置cell的內容
return cell;

節省內存方式就是重用單元格
如若表視圖顯示10個單元格,創建11個就能滿足需求
 ·首先,定義一個靜態字符串常量,指定一個標識符
 ·其次,檢查表視圖中是否存在閒置單元格,如果有取出來,如果沒有則爲nil
 ·如若不存在,將會創建一個新的cell,並且指定一個標識符 


單元格類型

·第一種單元格類型
·UITableViewCellStyleDefault

if (cell == nil) {
   cell = [[[UITableViewCell alloc]  initWithStyle: UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:@"t.png"];
cell.textLabel.text = text;



·第二種類型
·UITableViewCellStyleSubtitle

if (cell == nil) {
   cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:identifier]  autorelease];
}
cell.imageView.image = [UIImage imageNamed:@"t.png"];
cell.textLabel.text = text;
cell.detailTextLabel.text =detailText;





單元格的輔助圖標類型

·設置輔助圖標(默認爲None)

·輔助圖標樣式1

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


·輔助圖標樣式2

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;



·輔助圖標樣式3

cell.accessoryType = UITableViewCellAccessoryCheckmark;



·創建單選表視圖
·核心代碼

NSIndexPath *lastIndexPath =[NSIndexPath indexPathForRow:isSelected inSection:0];
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndexPath];
lastCell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
isSelected = indexPath.row;

·單元格高度自適應

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
    NSString *text = [_data objectAtIndex:indexPath.row];
    CGSize size = [text sizeWithFont: [UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300,1000)];
    return size.height+20;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章