标签:默认 nim 判断 anim tin 开关 animation gate 位置
// 设置表格的编辑状态
myTableView.editing = YES;
// 翻转表格的编辑状态
myTableView.editing = !myTableView.editing;
// 带动画翻转表格的编辑状态
[myTableView setEditing:!myTableView.editing animated:YES];
// UITableViewDelegate 协议方法
/*
默认为 Delete
*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了关注");
// 收回左滑出现的按钮(退出编辑模式)
tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[[myDataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// 按钮从右向左的顺序排列
return @[action1, action0];
}
/*
UITableViewCellEditingStyleNone; // 无
UITableViewCellEditingStyleDelete; // 删除模式,默认
UITableViewCellEditingStyleInsert; // 插入模式
UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; // 多选模式
*/
// UITableViewDelegate 协议方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// 删除、插入、多选删除,不设置默认时为删除
if (0 == indexPath.section) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleInsert;
}
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadData];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadData];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
// 表格删除或插入,默认为删除模式,写入该方法即表示允许删除
// UITableViewDataSource 协议方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// 判断编辑风格,默认是删除
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 表格删除
// 从数据源里删除
[[myDataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
// 从 tableView 里删除 cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
Person *person = [[Person alloc] init];
person.name = @"xiao bai";
person.age = 18;
// 表格插入
// 插入到数据源中
[[myDataArray objectAtIndex:indexPath.section] insertObject:person atIndex:indexPath.row];
// 插入一个 cell
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
[tableView reloadData];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
// 写入该方法即表示允许移动
// UITableViewDataSource 协议方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// 找到需要移动的对象
Person *person = [[myDataArray objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
// 从原始位置删掉
[[myDataArray objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
// 插入到新位置
[[myDataArray objectAtIndex:destinationIndexPath.section] insertObject:person atIndex:destinationIndexPath.row];
// 刷新 tableView
[tableView reloadData];
}
标签:默认 nim 判断 anim tin 开关 animation gate 位置
原文地址:https://www.cnblogs.com/CH520/p/9420423.html