一、UITableView的概念:
UITableView 是iOS中最重要的控件,幾乎所有的頁(yè)面都可以用UITableView完成。
tableView的使用需要遵循代理和數(shù)據(jù)源,這也是一種非常棒的設(shè)計(jì)模式,數(shù)據(jù)源模式可以近似為代理模式。
tableview要引入2個(gè)代理UITableViewDelegate,UITableViewDataSource
二、UITableView的基本用法:
1、基本屬性:
(1)設(shè)置tableview的類型
UITableViewStylePlain? ? ? ? ? ? 基本類型,分區(qū)頭標(biāo)題會(huì)懸浮
UITableViewStyleGrouped? ? 分組的類型,分區(qū)頭標(biāo)題不會(huì)懸浮
//初始化:
UITableView? *tableView =? [[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)? style:UITableViewStylePlain];
(2)設(shè)置背景:
tableView.backgroundColor = [UIColor redColor];
(3)設(shè)置分割線:
類型:tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
顏色:tableView.separatorColor = [UIColor blackColor];
位置:tableView. separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
(4)數(shù)據(jù)源和代理:
//主要管視圖內(nèi)容
tableView.delegate = self;
//tableView里面的數(shù)據(jù)管理
tableView.dataSource = self;
[self.view addSubview:tableView];
2、設(shè)置 UITableViewDelegate的方法:
//每個(gè)section有多少rows:(必須實(shí)現(xiàn)的方法)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//返回指定的 row 的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//返回指定的 section的header view 的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//返回指定的 section的footer view 的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
//返回指定的row 的cell:(必須實(shí)現(xiàn)的方法)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
eg:
static NSString* cellname = @"cellname";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//從tableveiw的復(fù)用池,是不是有可以復(fù)用的cell
//dequeueReusableCellWithIdentifier
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];
//初始化cell
if (cell == nil) {
//xib文件的初始化
cell = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]lastObject];
}
//沒有點(diǎn)擊效果::
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label.text = self.arr_tv[indexPath.row];
//自定義字體的uifont
cell.label.font = [UIFont fontWithName:self.arr_tv[indexPath.row] size:13];
//? ? NSLog(@"cellForRowAtIndexPath-%@",cell);
return cell;
}
//? 取消選中的效果::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消選中
//其他代碼
}
//返回指定的 section 的header的高度;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// 設(shè)置section的數(shù)量,默認(rèn)為1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//section由兩部分組成,header和footer,分別設(shè)置各自需要展示的字符串,也可以自定義顯示的樣式,需要用到其他的方法
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
eg:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來(lái)顯示標(biāo)題
case 1:
return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來(lái)顯示標(biāo)題
default:
return @"Unknown";
}
}
// 哪些row可以被編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// Moving/reordering? 移動(dòng)
// 哪些row可以被移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//如何設(shè)置tableview 可以被編輯
[TableView setEditing:YES animated:YES];
如果要退出編輯模式,肯定就是設(shè)置為NO
// - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回當(dāng)前cell? 要執(zhí)行的是哪種編輯,下面的代碼是 返回 刪除? 模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//? ? ? ? -(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告訴用戶編輯了 哪個(gè)cell,對(duì)應(yīng)上面的代碼,我們?cè)谶@個(gè)函數(shù)里面執(zhí)行刪除cell的操作。
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
[chatArray? removeObjectAtIndex:indexPath.row];
[chatTableView? reloadData];
}
//如何獲得 某一行的CELL對(duì)象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
3、查看代理的方法:
// 設(shè)置對(duì)應(yīng)的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// 自定義header和footer
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
// 設(shè)置編輯的樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;