editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)
2016-06-30 15:58
417 查看
ios8 出来的左滑小菜单 可以自定义想要的按钮 (要求ios8以上)
可以在导航栏右边放编辑按钮,删除操作
导航栏右边放编辑按钮,插入操作
导航栏右边放编辑按钮,移动操作
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"-----------=点击删除"); }]; UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"-----------=点击编辑"); }]; UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"-----------=点击置顶"); }]; UITableViewRowAction *signAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"-----------=标记"); }]; // 毛玻璃效果 deleteAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; editAction.backgroundColor = [UIColor blueColor]; topAction.backgroundColor = [UIColor grayColor]; signAction.backgroundColor = [UIColor yellowColor]; return @[deleteAction, editAction, topAction, signAction]; }
可以在导航栏右边放编辑按钮,删除操作
-(void)delete:(UIBarButtonItem *)sender { if (self.tableView.editing == NO) { self.tableView.editing = YES; sender.title = @"完成"; }else if (self.tableView.editing == YES) { self.tableView.editing = NO; sender.title = @"编辑"; } } // 设置tableView是否可以编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } // 设置删除操作时候的标题 -(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; } // 编辑模式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { [self.arrayM removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; } }
导航栏右边放编辑按钮,插入操作
// 设置tableView是否可以编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleInsert) { // 我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil]; // 同样,将数据加到list中,用的row [self.arrayM insertObject:@"新添加的行" atIndex:indexPath.row]; [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } }
导航栏右边放编辑按钮,移动操作
// 设置编辑模式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } // 这个方法用来告诉表格 这一行是否可以移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } // 这个方法就是执行移动操作的 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *) sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSUInteger fromRow = [sourceIndexPath row]; NSUInteger toRow = [destinationIndexPath row]; id object = [self.arrayM objectAtIndex:fromRow]; [self.arrayM removeObjectAtIndex:fromRow]; [self.arrayM insertObject:object atIndex:toRow]; }
相关文章推荐
- iOS支付宝1分钟集成,使用方法封装。超级好用
- iOS开发中下拉列表时放大列表顶部图片
- iOS内存管理:strong,weak,retain,assign
- iOS 小记
- iOS 开发 base64 加密和解密
- iOS--错误集锦--clang error linker command failed with exit code 1 use -v to see invocation
- iOS 动态下载系统的中文字体
- iOS进制转换
- iOS xcode使用断点追踪后,无法nslog,无法po对应的值 方法小结
- iOS开发 - 获取推送证书
- 手动创建WorkSpace方式的iOS多工程联合编译包含Static Library
- iOS 截屏功能的实现
- ios block 初探
- 经验总结帖|那些年,iOS开发踩过的坑
- iOS中帮你轻松code之可复用代码块
- iOS开发中设置tabbar选中图标的颜色
- 关于GCD中串行队列、并发队列和同步执行、异步执行的探讨
- iOS canOpenURL: failed for URL
- iOS 获取通讯录相关信息
- 『零行代码』解决键盘遮挡问题(iOS)