- cell的编辑模式
self.tableView.allowsMultipleSelection = YES; //允许多选
self.tableView.allowsSelection = YES; //允许选择某cell
self.tableView.allowsSelectionDuringEditing = YES; //编辑模式下允许可以
self.tableView.allowsMultipleSelectionDuringEditing = YES; //编辑模式下是否可以多选,选中后前面出现对勾
(如下图)
self.tableView.indexPathForSelectedRow; // 被选中的行
-
cell右滑插入左滑删除
//自定义cell的编辑模式 - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if(){ return UITableViewCellEditingStyleDelete; //在某行添加删除功能 }else{ return UITableViewCellEditingStyleInsert; //在某行添加插入功能 } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; //设置为可编辑模式 tableview默认 editing为NO } //进入编辑模式,确定编辑提交操作时,执行的代理方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //删除行 if (editingStyle == UITableViewCellEditingStyleDelete) { //一定先该行删除数据 NoticeItem *noticeItem = [_dataSourceArr objectAtIndex:indexPath.row]; [_http deleteNotificationAction:noticeItem.noticeId]; [_dataSourceArr removeObject:noticeItem]; //再去删除此行 dispatch_async(dispatch_get_main_queue(), ^{ [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; }); } //插入行 if(editingStyle == UITableViewCellEditingStyleInsert) { //可选择插入的数据 [_dataSourceArr insertObject:noticeItem atIndex:[_dataSourceArr objectAtIndex:indexPath.row]]; //插入行 [_tableView beginUpdates]; [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; num++; [_tableView endUpdates]; } }
-
移动cell位置
//cell右边出现编辑按钮,长按可拖动 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //实现数据源的排序更换 [_dataSourceArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; }
-
如果想要左滑出现几个自定义按钮 (参考http://blog.csdn.net/lengshengren/article/details/44243473 ):
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { //设置删除按钮 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) { [self.dataSource removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; }]; //设置收藏按钮 UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) { collectRowAction.backgroundColor = [UIColor greenColor]; NSLog(@"收藏成功"); }]; //设置置顶按钮 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) { //移动数据位次 [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0]; //改变行的位置 NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section]; [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath]; }]; collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; topRowAction.backgroundColor = [UIColor blueColor]; collectRowAction.backgroundColor = [UIColor grayColor]; return @[deleteRowAction,collectRowAction,topRowAction]; }
想要滑动出现的按钮含文字和图片,可以使用 MGSwipeTableCell
参考: http://www.jianshu.com/p/25908a61f849