UITableView 編輯模式
相關(guān)方法
UITableView
插入赏枚、刪除和移動(dòng)表格的行和分區(qū) (Inserting, Deleting, and Moving Rows and Sections)
//對(duì)表格執(zhí)行多個(gè)連續(xù)的插入筒饰、刪除和移動(dòng)操作前,調(diào)用開(kāi)始更新
- (void)beginUpdates;
//對(duì)表格執(zhí)行多個(gè)連續(xù)的插入、刪除和移動(dòng)操作后,調(diào)用結(jié)束更新
- (void)endUpdates;
//在一個(gè)或多個(gè)indexPath處插入cell
- insertRowsAtIndexPaths: withRowAnimation:
//刪除一個(gè)或多個(gè)indexPath處的cell
- deleteRowsAtIndexPaths: withRowAnimation:
//將指定indexPath對(duì)應(yīng)的cell移動(dòng)到另一個(gè)indexPath
- moveRowAtIndexPath: toIndexPath:
//在表格中用所選的動(dòng)畫插入一個(gè)或多個(gè)分區(qū)
- insertSections: withRowAnimation:
//在表格中用所選的動(dòng)畫刪除一個(gè)或多個(gè)分區(qū)
- deleteSections: withRowAnimation:
//將指定分區(qū)移動(dòng)到另一個(gè)位置
- moveSection: toSection:
Managing the Editing of Table Cells
//默認(rèn)值NO. YES則進(jìn)入編輯狀態(tài)
BOOL editing
- setEditing: animated:
UITableViewDataSource
插入或刪除表格行 (Inserting or Deleting Table Rows)
//對(duì)指定cell編輯完成時(shí)觸發(fā)
- tableView: commitEditingStyle: forRowAtIndexPath:
//返回值決定指定indexPath對(duì)應(yīng)的cell是否可編輯
- (BOOL)tableView: canEditRowAtIndexPath:
重新排列表格行 (Reordering Table Rows)
//返回值決定指定indexPath對(duì)應(yīng)的cell是否可移動(dòng)
- (BOOL)tableView: canMoveRowAtIndexPath:
//該方法告訴DataSource將指定的cell移動(dòng)到另一個(gè)位置
//單元格的移動(dòng)相關(guān)
- tableView: moveRowAtIndexPath: toIndexPath:
UITableViewDelegate
編輯表格行 (Editing Table Rows)
//開(kāi)始編輯某個(gè)cell時(shí)觸發(fā)
- tableView: willBeginEditingRowAtIndexPath:
//完成編輯某行時(shí)觸發(fā)
- tableView: didEndEditingRowAtIndexPath:
//返回值決定了該cell的編輯狀態(tài)
- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath:
//返回值將作為刪除指定cell時(shí)確定按鈕的文本
- (NSString *)tableView: titleForDeleteConfirmationButtonForRowAtIndexPath:
//返回的BOOL值決定指定cell處于編輯狀態(tài)時(shí),該cell是否應(yīng)該縮進(jìn)
- (BOOL)tableView: shouldIndentWhileEditingRowAtIndexPath:
實(shí)例
@interface TableViewController () {
// 記錄當(dāng)前正在執(zhí)行的操作. 0代表刪除, 1代表插入
NSInteger _action;
NSMutableArray *_list;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *insertBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *deleteBtn;
@end
@implementation ViewController
- (IBAction)Edit:(UIBarButtonItem *)sender {
if ([sender.title isEqualToString:@"刪除"]) {
_action = 0;
} else {
_action = 1;
}
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
if (self.tableView.isEditing) {
self.insertBtn.title = @"完成";
self.deleteBtn.title = @"完成";
} else {
self.insertBtn.title = @"插入";
self.deleteBtn.title = @"刪除";
}
}
- (void)viewDidLoad {
[super viewDidLoad];
_list = [NSMutableArray arrayWithObjects:@"美國(guó)", @"日本", @"中國(guó)", @"韓國(guó)", @"朝鮮", @"英國(guó)", nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseID = @"reuseID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
cell.textLabel.text = _list[indexPath.row];
return cell;
}
// 返回值決定指定indexPath對(duì)應(yīng)的cell是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// if (indexPath.row == 2) return NO;
return YES;
}
// 返回值決定指定indexPath對(duì)應(yīng)的cell是否可移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// 該方法告訴DataSource將指定位置的行移動(dòng)到另一個(gè)位置
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// 獲取將要移動(dòng)的數(shù)據(jù)
id targetObj = _list[sourceIndexPath.row];
// 從集合中刪除指定數(shù)據(jù)項(xiàng)
[_list removeObjectAtIndex:sourceIndexPath.row];
// 將移動(dòng)的數(shù)據(jù)項(xiàng)插入到指定位置
[_list insertObject:targetObj atIndex:destinationIndexPath.row];
}
// 編輯完成時(shí)觸發(fā)該方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleInsert) {
// 插入操作
// 將數(shù)據(jù)插入到集合中
[_list insertObject:_list[indexPath.row] atIndex:indexPath.row + 1];
// 在界面上插入一行
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
} else {
// 刪除操作
// 從集合中刪除數(shù)據(jù)項(xiàng)
[_list removeObjectAtIndex:indexPath.row];
// 從界面上刪除指定行
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark - UITableViewDelegate
// 返回值決定了該cell的編輯狀態(tài)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return _action == 0 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}
// 返回值將作為刪除指定cell時(shí)確定按鈕的文本
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"確定刪除";
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者