简体   繁体   English

UITableViewCell,显示滑动式删除按钮,无需滑动

[英]UITableViewCell, show swipe-style delete button without swipe

I search couple of article but I didn't find what I'm looking for. 我搜索几篇文章,但我找不到我要找的东西。 Basically, I want to show delete button on each row but I don't want use UITableView.editing property. 基本上,我想在每一行显示删除按钮,但我不想使用UITableView.editing属性。 Because it looks like this; 因为它看起来像这样;

在此输入图像描述

There will be "Edit" button. 将有“编辑”按钮。 When user click on it, delete button will looks like swipe-style. 当用户点击它时,删除按钮看起来像滑动式。

Is there any chance to show delete buttons like this; 有没有机会显示这样的删除按钮;

在此输入图像描述

Maybe there is a some way to deal with it. 也许有一些方法可以解决它。 Otherwise, I'm going to create custom view for this. 否则,我将为此创建自定义视图。

Thanks for your advice. 谢谢你的建议。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

       //Do something... 
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;

}
  • Add a boolean property: @property BOOL didPressEdit; 添加布尔属性: @property BOOL didPressEdit;
  • Add UIButton to UITableViewCell UIButton添加到UITableViewCell
  • When pressing edit, didPressEdit becomes TRUE and UITableView reloads, such that cell.deleteButton.hidden = !didPressEdit; 当按下编辑时, didPressEdit变为TRUE并且UITableView重新加载,例如cell.deleteButton.hidden = !didPressEdit; which makes all Delete buttons available 这使得所有删除按钮都可用
  • When pressing delete, remove object from datasource array, reload tableview 按delete时,从数据源数组中删除对象,重新加载tableview

Hope this helps 希望这可以帮助

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        [self.heartCartTabel setEditing:NO];
        [self editButtonClicked:indexPath.row];
    }];
    moreAction2.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

        [self tableView:self.heartCartTabel commitEditingStyle: UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
        //        [self.heartCartTabel deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    return @[deleteAction, moreAction2];
}

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

- (IBAction)editButtonClicked:(int)indexNumber {

}

You can use a UITableView delegate method to ask for those actions. 您可以使用UITableView委托方法来请求这些操作。 Implement this method as follows: 实现此方法如下:

  - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     // Respond to the action.
     }];
    modifyAction.backgroundColor = [UIColor blueColor];
    return @[modifyAction];
}

You can of course return multiple actions and customize the text and background color. 您当然可以返回多个操作并自定义文本和背景颜色。

Implementing this method is also required to make the row editable: 要使行可编辑,还需要实现此方法:

   - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath   {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [self.objects removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
     }

You need to call the method editActionsForRowAtIndexPath on your edit button click. 您需要在编辑按钮上单击调用方法editActionsForRowAtIndexPath。

  -(void)buttonTouched:(id)sender{

        UIButton *btn = (UIButton *)sender;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
        [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
    }

  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM