在没有删除按钮的情况下重新排序表视图单元格,并实现滑动以删除行

Nil*_*far 6 editing objective-c uitableview

我阅读了关于表视图和它的编辑样式,但我遇到了一些问题,因为只有三种编辑风格如下:

  1. UITableViewCellEditingStyleNone
  2. UITableViewCellEditingStyleDelete
  3. UITableViewCellEditingStyleInsert

我想重新排序我使用它的委托成功实现的tableview单元格.

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
   return UITableViewCellEditingStyleNone;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   //handle the editing style
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
  //move cells
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

我不想使用UITableViewCellEditingStyleDelete它,因为它在表视图上显示一个红色的圆形按钮.而不是这个,我想要一起刷卡到删除和重新排序功能.

有没有办法实现这个?

rma*_*ddy 1

这是可以做到的,但需要满足以下条件。滑动删除仅在表视图不处于编辑模式时才起作用,并且表重新排序仅在表视图处于编辑模式时才起作用。您将无法在表重新排序的同时进行滑动删除工作。

为了完成这项工作,您需要以下内容:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
   // Only allow deletion when the table isn't being edited
   return tableView.isEditing ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   // handle the row deletion
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    // Check if move is valid
    return proposedDestinationIndexPath;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView * nonnull)tableView moveRowAtIndexPath:(NSIndexPath * nonnull)fromIndexPath toIndexPath:(NSIndexPath * nonnull)toIndexPath {
    // process the moved row
}
Run Code Online (Sandbox Code Playgroud)

您将需要导航栏中的标准编辑按钮(或其他方式来切换表的编辑模式)。执行此操作的常见方法是在表视图控制器的方法中添加以下行viewDidLoad

self.navigationItem.rightBarButtonItem = [self editButtonItem];
Run Code Online (Sandbox Code Playgroud)