**雨中黃葉樹,燈下白頭人!**<草苗龜>
- UITableView繼承于UIScrollView,可以滾動(dòng)
- UITableView的每一條數(shù)據(jù)對(duì)應(yīng)的單元格叫做
cell
,是UITableView的一個(gè)對(duì)象,繼承于UIView. - UITableView可以分區(qū)顯示,每一個(gè)分區(qū)稱為
section
每一行稱為row,
編號(hào)都是從0開始. - 系統(tǒng)提供一個(gè)專門的類來整合section和row,叫做
NSIndexPath
.
代碼展示:
// 創(chuàng)建
UITableView *tableview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
[self.view addSubview:tableview];
// 每一行高
tableview.rowHeight = 50.0;
// 分割線顏色
tableview.separatorColor = [UIColor redColor];
// 分割線樣式
tableview.separatorStyle = 1;
// 頭部視圖
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
headView.backgroundColor = [UIColor yellowColor];
tableview.tableHeaderView = headView;
// 底部視圖
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
footView.backgroundColor = [UIColor greenColor];
tableview.tableFooterView = footView;
#pragma mark ----cell的重用機(jī)制
- 當(dāng) 個(gè)cell 被滑出屏幕,這個(gè)ce 會(huì)被系統(tǒng)放到相應(yīng)的重 池中。
2.當(dāng) tableview 需要顯 個(gè)cell ,會(huì)先去重 池中嘗試獲取 個(gè)cell沉删。
3.如果重 池沒有ce ,就會(huì)創(chuàng)建 個(gè)cell 幔翰。
4.取得ce 之后會(huì)重新賦值進(jìn) 使 。
// 第二種方法 :
// 第一步 : 注冊(cè)cell
// 參數(shù)1 : 是一個(gè)類(當(dāng)重用池中沒有cell的時(shí)候開始創(chuàng)建cell類)
// 參數(shù)2 : 給重用池一個(gè)重用標(biāo)示符
[tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
#pragma mark ----設(shè)置代理(處理數(shù)據(jù)和UI界面)
// 用于處理顯示視圖的相關(guān)內(nèi)容<UITableViewDelegate>
tableview.delegate = self;
// 用于處理數(shù)據(jù)相關(guān)的內(nèi)容<UITableViewDataSource>
tableview.dataSource = self;
// 設(shè)置每個(gè)區(qū)有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
// 設(shè)置分區(qū)數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
// 設(shè)置每行cell的樣式和數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* // 第一種
// 第一步 : 現(xiàn)在重用池中去取重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// 第二步 : 如果重用池中沒有,需要重新的創(chuàng)建一個(gè)
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
}
*/
// 第二種方法 (一定要注冊(cè)cell)
// 第二步 : 根據(jù)重用池標(biāo)示符去取重用池的cell在這使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// 設(shè)置文本
cell.textLabel.text = [NSString stringWithFormat:@"第 %ld 區(qū) --- 第 %ld 行",indexPath.section,indexPath.row];
return cell;
}
// 設(shè)置頭部標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"這是 %ld 區(qū)頭標(biāo)題",section];
}
// 設(shè)置尾部標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"這是 %ld 區(qū)尾標(biāo)題",section];
}
// 設(shè)置右側(cè)索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"a",@"b"];
}
// 設(shè)置每個(gè)區(qū)頭部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 150;
}
// 設(shè)置每行的高度 (可根據(jù)下標(biāo)AtIndexPath進(jìn)行專門的設(shè)置)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
return 100;
}
return 50;
}
// 設(shè)置尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 200;
}
結(jié)果展示部分