UITableView的用途
數(shù)據(jù)視圖
用來顯示大量相同格式數(shù)據(jù)的視圖
例如:通訊錄、好友列表、朋友圈
UITableView 基本用法
- 頭文件要遵守的協(xié)議:
UITableViewDelegate數(shù)據(jù)視圖的普通代理協(xié)議
UITableViewDataDelegate處理數(shù)據(jù)視圖的數(shù)據(jù)的代理協(xié)議 - 要知道的方法
alloc
initWithFrame(位置) style(風(fēng)格grouped or plain)
.delegate
.datasource - 定義一個數(shù)據(jù)視圖
- .m實現(xiàn)文件中創(chuàng)建數(shù)據(jù)視圖,并添加到當(dāng)前視圖中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創(chuàng)建數(shù)據(jù)視圖
//P1:數(shù)據(jù)視圖的位置
//P2:視圖的風(fēng)格
//UITableViewStylePlain 普通平鋪風(fēng)格
//UITableViewStyleGrouped 分組顯示風(fēng)格
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- 實現(xiàn)協(xié)議中@required要求實現(xiàn)的方法
-(NSInteger)tableView:(UITableView * )tableView numberOfRowsInSection:(NSInteger)section
獲取每組元素的個數(shù)(行數(shù))
-(UITableViewCell*)tableView:(UITableView * )tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
給每行填充數(shù)據(jù),即設(shè)置單元格
//獲取每組元素的個數(shù)(行數(shù))
//必須要實現(xiàn)的協(xié)議函數(shù)
//程序在顯示數(shù)據(jù)視圖時調(diào)用
//返回值 表示每組元素的個數(shù)
//P1:數(shù)據(jù)視圖對象本身
//P2:哪一組需要的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
//設(shè)置數(shù)據(jù)視圖的組數(shù)
//不是必須實現(xiàn)的方法,默認返回值為1組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellStr = @"cell";
//嘗試獲取可以復(fù)用的單元格,單元格足夠多的時候,劃出屏幕的單元格可以復(fù)用為剛滑進屏幕的單元格
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];
if(cell == nil){
//創(chuàng)建一個單元格對象
//P1:單元格的樣式
//P2:單元格的復(fù)用標記
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
}
NSString *str = [NSString stringWithFormat:@"%ld組%ld行",indexPath.section,indexPath.row];
//將單元格的文字內(nèi)容賦值
cell.textLabel.text = str;
return cell;
}
UITableView協(xié)議
用到的方法
heightForRowAtIndexPath 設(shè)置單元格的高度
titleForHeaderInSection 設(shè)置每組頭部的標題
titleForFooterInSection 設(shè)置每組尾部的標題
heightForHeaderInSection 設(shè)置頭部高度
heightForFooterInSection 設(shè)置尾部高度UITableViewDataDelegate處理數(shù)據(jù)視圖的數(shù)據(jù)的代理協(xié)議
例子:分A-Z個組,每組5個數(shù)據(jù)顯示在視圖聲明文件中先寫一個可變數(shù)組假裝一下源數(shù)據(jù)
NSMutableArray *_arrayData;.m實現(xiàn)文件中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創(chuàng)建數(shù)據(jù)視圖對象
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 536) style:UITableViewStyleGrouped];
//設(shè)置代理對象
_tableView.delegate = self;
//設(shè)置數(shù)據(jù)代理對象
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_tableView];
//按照數(shù)據(jù)視圖組數(shù)、行數(shù)給可變數(shù)組賦值
_arrayData = [[NSMutableArray alloc]init];
for(int i='A';i<'Z';i++){
NSMutableArray * arraySmall = [[NSMutableArray alloc]init];
for(int j=0;j<5;j++){
NSString *str = [NSString stringWithFormat:@"%c%d",i,j];
[arraySmall addObject:str];
}
[_arrayData addObject:arraySmall];
}
}
- 實現(xiàn)協(xié)議方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _arrayData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_arrayData[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = @"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:str];
if(cell ==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];
return cell;
}
//設(shè)置單元格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
//設(shè)置每組頭部標題
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"lalala";
}
//設(shè)置每組尾部的標題
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"尾部";
}
//設(shè)置頭部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
//設(shè)置尾部高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 60;
}
高級協(xié)議
- 用到的方法
autoresizingMask自動調(diào)整子視圖的大小
.tableHeaderView數(shù)據(jù)視圖的頭部視圖的設(shè)定
.tableFooterView數(shù)據(jù)視圖的尾部視圖
reloadData 更新數(shù)據(jù)視圖,更新加載數(shù)據(jù)
commitEditingStyle 可以顯示編輯狀態(tài),當(dāng)手指在單元格上移動時
didSelectRowAtIndexPath 選中單元格時
didDeselectRowAtIndexPath 取消選中單元格時
editingStyleForRowAtIndexPath 每個單元格左側(cè)顯示效果,默認為刪除
- UITableViewCell
tableView dequeueReusableCellWithIdentifier 獲取可以復(fù)用的單元格
alloc initWithStyle
(UITableViewCellStyleSubtitle 需要設(shè)置子標題時用,默認是Default)
cell.textLabel.text 設(shè)置單元格文字
cell.detailTextLabel.text = @"子標題"
cell.imageView.image = image 設(shè)置默認的圖片
數(shù)據(jù)視圖與導(dǎo)航視圖結(jié)合
- 在AppDelegate.m文件初始化后執(zhí)行的application方法中將導(dǎo)航控制器設(shè)置為根視圖控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
- ViewController.h中
@interface ViewController : UIViewController
<UITableViewDelegate,
UITableViewDataSource>
{
//數(shù)據(jù)視圖
UITableView* _tableView;
//數(shù)據(jù)源
NSMutableArray* _arrayData;
//添加導(dǎo)航按鈕
UIBarButtonItem* _btnEdit;
UIBarButtonItem* _btnFinish;
UIBarButtonItem* _btnDelete;
//設(shè)置編輯狀態(tài)
BOOL _isEdit;
}
@end
- ViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//自動調(diào)整子視圖的大小
_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
_tableView.delegate=self;
_tableView.dataSource=self;
//數(shù)據(jù)視圖的頭部視圖的設(shè)定
_tableView.tableHeaderView = nil;
//數(shù)據(jù)視圖的尾部視圖
_tableView.tableFooterView = nil;
[self.view addSubview:_tableView];
//初始化數(shù)據(jù)源數(shù)組
_arrayData = [[NSMutableArray alloc]init];
for(int i=1 ; i<20 ; i++){
NSString* str = [NSString stringWithFormat:@"A %d",i];
[_arrayData addObject:str];
}
//當(dāng)數(shù)據(jù)視圖的數(shù)據(jù)源發(fā)生變化時
//更新數(shù)據(jù)視圖,重新加載數(shù)據(jù)
[_tableView reloadData];
[self createBtn];
}
-(void)createBtn{
_isEdit = NO;
//創(chuàng)建功能按鈕
_btnEdit = [[UIBarButtonItem alloc]initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
_btnFinish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinish)];
_btnDelete = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];
self.navigationItem.rightBarButtonItem = _btnEdit;
}
//可以顯示編輯狀態(tài),當(dāng)手指在單元格上移動時
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//根據(jù)索引刪除數(shù)據(jù)源對應(yīng)的數(shù)據(jù)
[_arrayData removeObjectAtIndex:indexPath.row];
//數(shù)據(jù)源更新
[_tableView reloadData];
}
//選中單元格時
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"選中單元格 %ld, %ld",indexPath.section, indexPath.row);
}
//取消選中的時候
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"取消選中單元格 %ld, %ld",indexPath.section, indexPath.row);
}
//單元格顯示效果協(xié)議,默認為刪除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//插入狀態(tài) UITableViewCellEditingStyleInsert;
//空狀態(tài) UITableViewCellEditingStyleNone;
//多選狀態(tài) UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
return UITableViewCellEditingStyleDelete;
}
-(void)pressEdit{
_isEdit = YES;
self.navigationItem.rightBarButtonItem = _btnFinish;
[_tableView setEditing:YES];
self.navigationItem.leftBarButtonItem = _btnDelete;
}
-(void)pressDelete{
_isEdit = YES;
self.navigationItem.rightBarButtonItem = _btnEdit;
[_tableView setEditing:NO];
self.navigationItem.leftBarButtonItem = nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayData.count;
}
//默認組數(shù)返回1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* str = @"ID";
//嘗試獲取可以復(fù)用的單元格,單元格足夠多的時候,劃出屏幕的單元格可以復(fù)用為剛滑進屏幕的單元格
UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:str];
if(cell == nil){
//UITableViewCellStyleDefault 設(shè)置子標題也不會顯示
//UITableViewCellStyleSubtitle
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
//設(shè)置子文字標題
cell.detailTextLabel.text = @"子標題";
NSString* imageStr = [NSString stringWithFormat:@"icon%d",indexPath.row%3+1];
UIImage* image = [UIImage imageNamed:imageStr];
// UIImageView* imageView = [[UIImageView alloc]initWithImage:image];
//設(shè)置默認的圖片
cell.imageView.image = image;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}