UITableView(表格視圖)
UITableView是用一種表格的形式來(lái)顯示一組或者多組數(shù)據(jù)
UITableView繼承于UIScrollView,UITableView默認(rèn)值水平方向不能滾動(dòng),在垂直方向可以滾動(dòng)
一、單分組的UITableView
-
創(chuàng)建數(shù)據(jù)源
-
創(chuàng)建表格視圖
-
遵守協(xié)議:UITableViewDelegate卦方、UITableViewDataSource
-
重用標(biāo)志
-
二嬉愧、多分組的UITableView(略)
三浴骂、UITableView的刪除功能
-
點(diǎn)擊刪除按鈕調(diào)用的Delegate方法
-
修改刪除按鈕的文字
四废登、UITableView的插入和移動(dòng)功能
-
如果正在編輯,點(diǎn)擊結(jié)束編輯; 如果不在編輯,點(diǎn)擊進(jìn)入編輯狀態(tài)
-
修改返回編輯的狀態(tài): 此處返回插入數(shù)據(jù)的狀態(tài)
-
插入數(shù)據(jù)的操作
-
實(shí)現(xiàn)不同組不能交換的邏輯
一睁蕾、單分組的UITableView
-
創(chuàng)建數(shù)據(jù)源
-
創(chuàng)建表格視圖
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
-
遵守協(xié)議:UITableViewDelegate柒凉、UITableViewDataSource
-
協(xié)議方法中一些參數(shù)的意義
NSIndexPath:section row section:表示在表格的第幾組 row:表示在表格的第幾行
-
重用標(biāo)志
static NSString *cellId = @"cellId"; //從重用的reuseArray里面獲取 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; //沒(méi)有獲取到重用的cell族阅,就需要?jiǎng)?chuàng)建一個(gè)新的cell if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; NSLog(@"%ld",indexPath.row); }
-
二篓跛、多分組的UITableView
三膝捞、UITableView的刪除功能:左滑出現(xiàn)刪除按鈕
-
點(diǎn)擊刪除按鈕調(diào)用的Delegate方法: (實(shí)現(xiàn)該方法就能左滑出現(xiàn)"刪除"按鈕)
/* 第一個(gè)參數(shù):表格視圖對(duì)象 第二個(gè)參數(shù):編輯表格的方式 第三個(gè)參數(shù):操作的cell對(duì)應(yīng)的位置 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //如果是刪除 if (editingStyle == UITableViewCellEditingStyleDelete) { //點(diǎn)擊刪除按鈕調(diào)用這里的代碼 //數(shù)據(jù)源刪除 NSMutableArray *subArray = _dataArray[indexPath.section]; [subArray removeObjectAtIndex:indexPath.row]; //UI的刪除 //刪除表格視圖的某一個(gè)cell /* 第一個(gè)參數(shù):將要?jiǎng)h除的所有cell的indexPath組成的數(shù)組 第二個(gè)參數(shù):動(dòng)畫(huà) */ //@[indexPath]等價(jià)于 //[NSArray arrayWithObjects:indexPath, nil]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; //將整個(gè)表格視圖刷新 //[tableView reloadData]; } }
- 默認(rèn)實(shí)現(xiàn)了下述方法,默認(rèn)返回YES(因此可以不實(shí)現(xiàn))
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- 默認(rèn)實(shí)現(xiàn)了下述方法,默認(rèn)返回YES(因此可以不實(shí)現(xiàn))
-
修改刪除按鈕的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"刪除"; }
四、UITableView的插入和移動(dòng)功能
-
如果正在編輯,點(diǎn)擊結(jié)束編輯; 如果不在編輯愧沟,點(diǎn)擊進(jìn)入編輯狀態(tài)
[_tbView setEditing:!_tbView.editing animated:YES];
-
修改返回編輯的狀態(tài): 此處返回插入數(shù)據(jù)的狀態(tài)
默認(rèn)返回UITableViewCellEditingStyleDelete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
}
-
插入數(shù)據(jù)的操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleInsert) { //如果是插入 //點(diǎn)擊添加按鈕的操作 NSMutableArray *subArray = _dataArray[indexPath.section]; //添加一條數(shù)據(jù) StudentModel *model = [[StudentModel alloc] init]; model.name = @"我是新同學(xué)"; model.age = 18; [subArray insertObject:model atIndex:indexPath.row]; //刷新表格 [_tbView reloadData]; } }
-
實(shí)現(xiàn)不同組不能交換的邏輯
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { if (sourceIndexPath.section!=proposedDestinationIndexPath.section) { return sourceIndexPath; } return proposedDestinationIndexPath; }