UITableView

/*緩存

 今天須要掌握的性能優化

  1.UITableView以及兩種風格和三部分性能

 風格:優化

       UITableViewStylePlain普通風格spa

       UITableViewStyleGrouped分組風格代理

 三部分: 表頭   表尾   Cellxml

 2. UITableViewController對象

 就是一個自帶UITableView的控制器圖片

 3. UITableViewCell以及四種風格資源

 風格:

 * UITableViewCellStyleDefault

 圖片居左,textlabel在圖片右邊,detailTextLabel默認不顯示

 * UITableViewCellStyleValue1

 圖片居左,textlabel在圖片的右邊,detailTextLabel居右

 * UITableViewCellStyleSubtitle

 圖片居左,textLabel在圖片的右邊,deetailTextlabeltextlabel的下方。

 * UITableViewCellStyleValue2

 左邊一個主標題textLabel,挨着右邊一個副標題detailTextLabel

 經過代理給UITableView設置Cell

 性能優化

 經過代理添加刪除cell

*/

#pragma UITableView 風格 三部分

    /*

     1.UITableView 以及兩種風格和三部分

     風格:

     UITableViewStylePlain普通風格

     UITableViewStyleGrouped分組風格

     三部分:

     tableHeaderView 表頭 

     tableFooterView 表尾

            Cell

    表頭 表尾根據咱們的須要而選擇是否添加

     */

#import "ContactViewController.h"

@interface ContactViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *tabView1;

}

@end

- (void)viewDidLoad {

    [super viewDidLoad];

    UITableView *tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 414, 736) style:UITableViewStyleGrouped];

    tabView.backgroundColor = [UIColor grayColor];

    tabView.tableHeaderView = [self addHeaderView];//UITableView設置表頭

    tabView.tableFooterView = [self addFooterView];//UITableView設置表尾

    tabView.delegate = self;

    tabView.dataSource = self;

 [self.view addSubview:tabView];

}

//建立表頭視圖

-(UIView *)addHeaderView{

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];

    lable.text = @"表頭";

    lable.backgroundColor = [UIColor greenColor];

    

    return lable;

}

//建立表尾視圖

-(UIView *)addFooterView{

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];

    lable.text = @"表尾";

    lable.backgroundColor = [UIColor greenColor];

    

    return lable;

}

#pragma UITableViewController 

    /*

     就是一個自帶UITableView的控制器

     */

#pragma 經過代理給UITableView設置Cell  以及四種風格

     * UITableViewCellStyleDefault

     圖片居左,textlabel在圖片右邊,detailTextLabel默認不顯示

     * UITableViewCellStyleValue1

     圖片居左,textlabel在圖片的右邊,detailTextLabel居右

     * UITableViewCellStyleSubtitle

     圖片居左,textLabel在圖片的右邊,deetailTextlabeltextlabel的下方。

     * UITableViewCellStyleValue2

     左邊一個主標題textLabel,挨着右邊一個副標題detailTextLabel

     UITableViewCellAccessoryNone 啥也沒有

如下是關於cell右側的標記也是Accessory

     右邊出現小箭頭

     UITableViewCellAccessoryDisclosureIndicator;

     i加箭頭

    UITableViewCellAccessoryDetailDisclosureButton;

     對號

     UITableViewCellAccessoryCheckmark;

     i

    UITableViewCellAccessoryDetailButton;

     */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 20;

}// 定義多少行   -----代理實現方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    /*

     UITableView每次滑動 一定有消失的cell,系統會自動將這些消失的cell放到緩存池裏,須要新cell時,系統如今緩存池裏看是否有cell 有的就利用,沒有的就新建

     前提 UITableView滑動

     1.舊的cell消失,系統自動將這個cell放到緩存池裏

     2.新的cell要顯示就要用到代理方法

     2.1首先看看緩存池裏有沒有cell 

     2.2若是有就利用若是沒有就新建

     2.3返回cell

     */

     static NSString *cellId = @"cellID";

    UITableViewCell *cell = [tabView1 dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

    }

cell的三大屬性 textLabel   detailTextLabel    imageView

    cell.textLabel.text = @"標題";

    cell.detailTextLabel.text = @"副標題";

    cell.imageView.image = [UIImage imageNamed:@"圖片名字"];

     return cell;

    

}//每行cell的風格 每次出現新的cell就要觸發 -----代理方法

另外咱們的tableView還有一個分組格式

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{

    return 2;

}//指定有多少分組 ----代理方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 20;

}//改變cell的高度  ------cell的其餘的大小不能調整,只能調整它的高度,其餘系統默認爲起始位置(0.0)寬爲屏幕的寬

 UITableViewStylePlain  表頭不在tableView上通常表頭不會跟着cell移動
 UITableViewStyleGrouped 表頭跟着一塊兒動 有分隔在組組之間的
 須要分組cell的樣式必須使用initWithFrame:   style:初始化
 使用initWithFrame:初始化默認的是平鋪的樣式,不能後期更改樣式設置
 UITableViewDelegate 是tableView視圖相關 的代理 機制重用機制 滾筒原理  只有固定一屏的視圖,超出重用超出視圖的cell  只是更改他的數據
 UITableViewDataSource 是數據相關的代理方法
 DataSource有兩個必須實現的代理方法沒實現 就會出現警告 運行就崩潰 緣由沒有實現代理方法

對於TableView的一些代理方法,這些都挺經常使用的

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath//設置編輯狀態下的樣式
 UITableViewCellEditingStyleNone,沒有(移動)
 UITableViewCellEditingStyleDelete,刪除 編輯狀態會出現減號
 UITableViewCellEditingStyleInsert插入 編輯狀態會出現加號
 
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath //YES 能夠移動
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath//提交編輯的時候 調用
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath//專門用於移動的時候調用
 - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath//這是真的移動 移動視圖的時候要同時操做數據
 操做數據的步奏
 1.移除要移動的數據  先要保存資源數據 在移除
 2.把資源數據插入到 指定的移動位置
 
 [table reloadData]; UITableView的代理方法從新走一遍 table  是我本身定義的UITableView的一個對象;
相關文章
相關標籤/搜索