day12---UITableView

UITableView 表格視圖
一 UITableView
1.1是什么?
以列表的方式展示數(shù)據(jù)的一種控件,且繼承自UISrollView,默認是只能上下滾動;

1.2 使用步驟:
    1)創(chuàng)建UITableView對象,并設(shè)置樣式;
    2)配置要展示的數(shù)據(jù)和外觀;
        三問:
        1)有幾個分區(qū)?
        2)每個分區(qū)有多少行?
        3)每行的展示的內(nèi)容是什么?
    3)把它添加到父視圖上顯示;
見【Demo】-【1-UITableView】
  • (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;(消除導航控制器對其的影響)

    /********** 創(chuàng)建UITableView ************/
    //UITableViewStylePlain 普通平板樣式
    //UITableViewStyleGrouped 分組樣式
    UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, screenBounds.size.width, screenBounds.size.height-64) style:UITableViewStylePlain];

    myTableView.delegate = self;
    myTableView.dataSource = self;

    [self.view addSubview:myTableView];
    }

pragma mark -UITableViewDataSource

//三問 【** 重點 **】
//1問,告訴tableView應該顯示多少個分區(qū)【如果不實現(xiàn),默認是1個分區(qū)】

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
    }

//2問,告訴tableView每個分區(qū)顯示多少行 (該方法有幾個分區(qū)調(diào)用幾次)

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

    return [self.dataArray count];
    }

//3問,告訴tableView每一行顯示的具體內(nèi)容是什么

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

    //創(chuàng)建靜態(tài)的標識
    static NSString *cellID = @"MyCell";
    //先從tableView的復用池中去查找沒有帶cellID標識的單元格;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //如果沒有找到(復用池中沒有符合條件的單元格)
    if (cell == nil) {
    //自己創(chuàng)建一個新的單元格,并初始化單元格的樣式,給它加上標識
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }
    //什么是indexPath?indexPath.row?
    MyModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.title;

    //返回符合條件的單元格對象;
    return cell;
    }

1.3 cell簡介 【重點】
UITableView的每一行都是一個UITableViewCell類型的對象,通過dataSource的第三問來初始化每一行;

【cell 的復用機制】
當滾動列表時候,部分UITableViewCell會移出窗口;UITableView會將移出去的UITableViewCell放入到一個復用池中,等待重用,當UITableView要求返回UITableViewCell的時候,會先查看復用池,如果池中有未使用的病符合條件的UITableViewCell,就會直接拿來使用,并重新配置這個UITableViewCell展示的內(nèi)容,然后返回給UITableView,如果池子中沒有符合條件的cell,那么則創(chuàng)建一個新的UITableViewCell對象,并返回!

cell的復用機制保證了值創(chuàng)建有限個cell的對象,來顯示無限條數(shù)據(jù),極大限度的節(jié)約了內(nèi)存的開銷,提高了性能,具有極大的借鑒意義;

1.4 UITableView的原理
a 先詢問有幾個分區(qū)

b 根據(jù)分區(qū)的個數(shù),再多次詢問每個分區(qū)有多少行,每次詢問的時候,會攜帶分區(qū)的section;

c 根據(jù)分區(qū)號及分區(qū)內(nèi)的行號,再多次詢問每一行顯示的內(nèi)容是什么,其中分區(qū)號section和行號row會組成一個NSIndexPath的類型,作為參數(shù)傳遞給方法;
  • (void)viewDidLoad {
    [super viewDidLoad];

    //1.請求數(shù)據(jù)
    [self reloadData];
    //2.創(chuàng)建UITableView
    [self creatUITableView];
    }
    -(void)creatUITableView
    {
    self.automaticallyAdjustsScrollViewInsets = NO;

    //添加背景圖片
    UIImageView *bgImg = [[UIImageView alloc] initWithFrame:screenBounds];
    bgImg.image = [UIImage imageNamed:@"5.jpg"];
    // [self.view addSubview:bgImg];

    UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, screenBounds.size.width, screenBounds.size.height-64) style:UITableViewStyleGrouped];
    //必寫
    myTableView.delegate = self;
    myTableView.dataSource = self;
    // myTableView.backgroundColor = [UIColor clearColor];
    myTableView.backgroundView = bgImg;

    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    headerView.image = [UIImage imageNamed:@"10.jpg"];
    headerView.contentMode = UIViewContentModeScaleAspectFit;
    UIImageView *footerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    footerView.image = [UIImage imageNamed:@"15.jpg"];
    footerView.contentMode = UIViewContentModeScaleAspectFit;
    //給myTableView設(shè)置頭部視圖
    myTableView.tableHeaderView = headerView;
    //給myTableView設(shè)置尾部視圖
    myTableView.tableFooterView = footerView;

    [self.view addSubview:myTableView];
    }

pragma mark -UITableViewDataSource

//3問 每行顯示的內(nèi)容是什么

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *cellID = @"cellName";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
    //系統(tǒng)給我們提供了3中Cell樣式
    // UITableViewCellStyleDefault,
    // UITableViewCellStyleValue1,
    // UITableViewCellStyleValue2,
    // UITableViewCellStyleSubtitle
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    //將數(shù)據(jù)源中的數(shù)據(jù),賦值給cell對象展示出來
    UserModel *model = self.dataArray[indexPath.section][indexPath.row];
    //1.顯示主標題
    cell.textLabel.text = model.name;
    //2.設(shè)置頭像
    cell.imageView.image = [UIImage imageNamed:model.iconImage];

    //設(shè)置圓角
    cell.imageView.layer.cornerRadius = 8;
    cell.imageView.layer.masksToBounds = YES;
    // cell.imageView.layer.borderColor = (__bridge CGColorRef)([UIColor redColor]);
    // cell.imageView.layer.borderWidth = 10;

    //3.顯示副標題
    cell.detailTextLabel.text = model.detailTitle;

    //設(shè)置cell右側(cè)的提示樣式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor clearColor];

    return cell;
    }

/**************************** 相關(guān)屬性 *******************************/
//設(shè)置分區(qū)cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

//設(shè)置分區(qū)的頭部視圖的標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"%c區(qū)的頭部標題",(char)('A'+section)];

}

//設(shè)置分區(qū)的尾部標題
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"%c區(qū)的尾部標題",(char)('A'+section)];
}

/****************** 自定義分區(qū)的頭部,尾部視圖 **********************/
//當同時設(shè)置分區(qū)的頭部標題和頭部視圖,那么優(yōu)先顯示頭部視圖
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = [NSString stringWithFormat:@"%c區(qū)的頭部標題",(char)('A'+section)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:20];
label.backgroundColor = [UIColor cyanColor];
label.textColor = [UIColor redColor];
return label;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = [NSString stringWithFormat:@"%c區(qū)的尾部標題",(char)('A'+section)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:15];
label.backgroundColor = [UIColor magentaColor];
label.textColor = [UIColor yellowColor];
return label;
}

/********************* 設(shè)置分區(qū)的頭部視圖的高度 ****************/
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 50;
}

//設(shè)置右邊索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *mArr = [NSMutableArray array];
for (int i='A'; i<='Z'; i++) {
NSString *str = [NSString stringWithFormat:@"%c",i];
[mArr addObject:str];
}
return mArr;
}

1.5 UITableViewController 表格視圖控制器
UIViewController 管理視圖的
UINavigationController 管理多個UIViewController
UITabBarController 即可以管理導航控制器,也可以管理UIViewController

2.1 概念
UITableViewController管理的是一個UITableView;
為了配合UITableView的使用而專門設(shè)立的一種特殊控制器,專門用來管理表格視圖;

2.2 特點:
1)繼承自UIViewController

2)自帶的視圖已經(jīng)是UITableView類型,通過 .tableView的屬性來訪問自己攜帶的表格視圖;

3)控制器本身已經(jīng)默認遵守了UITableViewDelegate和UITableViewDataSource,并且設(shè)置了自己作為該表格式圖的代理對象;

4)創(chuàng)建一個表格式圖控制器類,繼承自UITableViewController,在這個類中專心處理相關(guān)代理方法【最主要的三問】;
見【Demo】-【3-UITableViewController】
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //從表格視圖的復用池中按照之前注冊指定的標識去取可重用的對象;
    //該方法一定會返回一個不為nil的對象;
    //因為:復用池中如果沒有可重用的單元格,系統(tǒng)會根據(jù)之前注冊的類,自動的創(chuàng)建一個單元格對象并返回;
    //如果復用池中有可重用的單元格,系統(tǒng)會直接返回;
    //優(yōu)點:自動處理一切細節(jié),減少代碼量;
    //缺點:這種形式返回的cell對象,只能是默認的樣式,其他的三種樣式無法設(shè)置;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

    //取出數(shù)據(jù)源中對應的數(shù)據(jù)模型,并展示在cell上
    StudentModel *model = self.dataArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.name;
    cell.imageView.image = [UIImage imageNamed:model.icon];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
    }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}

pragma mark -UITableViewDelegate

//一答 【** 重點 **】
//當選中cell的時候,調(diào)用該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//拿到當前點擊的cell所對應的數(shù)據(jù)模型
StudentModel *model = self.dataArray[indexPath.section][indexPath.row];

//跳轉(zhuǎn)到詳情頁面
DeatilViewController *dvc = [[DeatilViewController alloc] initWithNibName:@"DeatilViewController" bundle:nil];
//把這個model屬性傳值給詳情頁面
dvc.model = model;
__block MyTableViewController *weakself = self;
dvc.block = ^{
    //刷新當前的tableView(系統(tǒng)自帶的方法)
    [weakself.tableView reloadData];
    
};
[self.navigationController pushViewController:dvc animated:YES];

}

關(guān)于單元格的實現(xiàn)方法:
方式一:
1)定義靜態(tài)標識
2)根據(jù)標識從復用池中取對象
3)如果有,重新賦值后,直接使用
4)如果沒有,那么新建一個單元格
方法二:
1)在viewDidLoad中,提前注冊號單元格,并指定標識
2)在第三問時,直接從復用池中取,就算沒有可重用的單元格,系統(tǒng)內(nèi)部會根據(jù)一早注冊號的單元格樣式創(chuàng)建單元格,并返回,如果有可重用的,直接返回;
《但方法二生成的單元格樣式只能是默認樣式,其他樣式無法設(shè)置》

兩種設(shè)置cell顯示內(nèi)容的方式: 【** 重點 **】

第一種:
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //創(chuàng)建靜態(tài)的標識
    static NSString *cellID = @"MyCell";
    //先從tableView的復用池中去查找沒有帶cellID標識的單元格;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //如果沒有找到(復用池中沒有符合條件的單元格)
    if (cell == nil) {
    //自己創(chuàng)建一個新的單元格,并初始化單元格的樣式,給它加上標識
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }
    //什么是indexPath?indexPath.row?
    MyModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.title;

    //返回符合條件的單元格對象;
    return cell;
    }

第二種:
//提前注冊cell

[cView registerClass:[MyCell class] forCellWithReuseIdentifier:@"Cell"];

//3問 每一項顯示什么內(nèi)容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//從復用池中取有沒有符合要求的單元格,如果有,直接返回使用,如果沒有,系統(tǒng)會根據(jù)之前注冊的單元格,自動的創(chuàng)建一個單元格對象,并返回;
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//因為UICollectionViewCell是系統(tǒng)自帶的cell,所以沒有提供樣式,沒有提供對應的空間,只能設(shè)置相關(guān)屬性;
cell.model = self.dataArray[indexPath.section][indexPath.row];

cell.backgroundColor = [UIColor orangeColor];


return cell;

}

如果cell用xib寫的話,提前注冊用下面這種寫法:
[self.tableView registerNib:[UINib nibWithNibName:@"AppCell" bundle:nil] forCellReuseIdentifier:@"AppCellID"];

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AppCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppCellID" forIndexPath:indexPath];

    cell.model = self.dataArray[indexPath.row];

    return cell;
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,307評論 3 38
  • 前言 最近忙完項目比較閑,想寫一篇博客來分享一些自學iOS的心得體會,希望對迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,600評論 9 5
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,750評論 1 14
  • { 24、Sqlite數(shù)據(jù)庫 1、存儲大數(shù)據(jù)量,增刪改查,常見管理系統(tǒng):Oracle、MSSQLServer、DB...
    CYC666閱讀 1,055評論 0 1
  • 一、簡介 官方給出了比較全面的介紹,要點摘錄如下: table view的作用:導航、展示索引列表、展示詳情信息、...
    quantiza閱讀 827評論 0 1

友情鏈接更多精彩內(nèi)容