UITableView 的编辑
2015-08-13 19:28
381 查看
准备工作
@property(nonatomic, retain)UITableView *tableView @property(nonatomic, retain)NSMUtableArray *arr - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 365, 665 - 64) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.delegate = self; self.tableView.dataSource = self; self.navigationItem.rightBarButtonItem = 0 } // 方法 // 重写系统的编辑按钮, 点击触发方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:Yes]; } // 设置哪些行科技进行编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // 奇数行可以编辑, 偶数行不能编辑 if (indexPath.row % 2 == 0) { return NO; } else { return YES; } } // 样式: 两种样式, 一个是插入, 一个是删除 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (index.row % 2 == 1){ return UITableViewCellEditingStyleDelete; } return UITableViewCellStyle } // 删除数据 - (void)tableView:(UITableVIew *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.arr removeObjectAtIndex:indexPath.row}; [self.tableView reloadDate]; 或 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } if (editingStyle == UITableViewCellEDitingStyleInsert) { [self.arr insertObject:@"字符串" atIndex:indexPath.row]; [self.tableView reloadData]; 或 [self.tableView insertRowAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } // 修改删除按键 - (NSString *)tableView:(UITableView *)tableView titleForDeleteContirmationButtonForRowAtIndexPath:(NSIndexPath *)indexpath { return @"来点我啊"; } // 这个方法是iOS8.0 之后出现的方法, 可以在编辑的状态时候有多个按钮 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowactionStyleDelfault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ // 按键的点击触发的事件都写在block中 NSLog(@"出发了删除按钮"); [self.arr removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationLet]; }]; UITableViewRowAction *addAction = [UITableViewRowAction rowActWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ NSLog(@"你是谁"): [self.arr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0]; [self.tableView reloadData]; }]; deleteAction.backgroundColor = [UIColor grayColor]; return @[deleteAction addAction]; } // 移动 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 1. 现获取起始位置的数据 NSString *str = [self.arr{sourceIndexPath.row] retain]; // 2. 把起始位置的对象从数据中离开 [self.arr removeObjectAtIndex:sourceIndexPath.row]; // 3. 把数据插入到数组的目的位置上去 [self.arr insertObject:str atIndex: destinationIndexPath.row]; [self.tableView relocate]; [str release]; }
相关文章推荐
- UITextField 详解
- 配置UIAutomator中的ANT环境报不是内部命令与外部命令
- SOAPUI 循环设置
- SOAPUI 属性传递之二
- SOAPUI 数据源设置
- iOS开发UI中懒加载的使用方法
- UITableViewCell左侧会有默认15像素的空白
- SOAPUI 属性设置
- 关于自定义UITableViewCell布局点击Cell消失的问题
- 手写输入法输入 [UIKBBlurredKeyView candidateList]
- 获取UITableView每行中不同的UITextField输入的内容
- MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated错误
- 【Android和iOS】快速切换到主线程更新UI
- UITabBarController
- AutoLayout初战----Masonry与FDTemplateLayoutCell实践
- UITextField总结
- UI 06 ScrollView 的手动循环播放 与 自动循环播放
- 判断UITableView是否滑到底部
- UILabel 总结
- UITextField