for_2012 2013-03-27 05:40 采纳率: 0%
浏览 2046

UITableView从右至左滑动

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if(editingStyle == UITableViewCellEditingStyleDelete){
        //[theSimpsons removeObjectAtIndex:indexPath.row];

        NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"];
        NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"];
        DBManager *dbm = [[DBManager alloc] init];
        [dbm initiallzeDatabase];
        [dbm deleteItemAtIndexPath:time :date];

        //arrayList = [dbm getParkResults];
        //[parkTableView reloadData];

        [arrayList removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

这是我应用中的部分代码,编辑部分很正常,只是删除键只有在单元中从左至右滑动时才显示。现在我有一个从左至右打开的菜单,我想让用户从右至左滑动时,删除键也会显示。

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-19 23:00
    关注

    ChatGPT尝试为您解答,仅供参考
    您可以通过实现表视图的 tableView:leadingSwipeActionsConfigurationForRowAtIndexPath: 方法来实现从右向左滑动时显示删除按钮。

    这是您可以使用的一个例子:

    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Create a delete action
        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            // Perform delete action here
            // Remove the item from the data source
            [arrayList removeObjectAtIndex:indexPath.row];
            // Delete the row from the table view
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Call completion handler to dismiss the action button
            completionHandler(YES);
        }];
        // Create and return a swipe actions configuration with the delete action
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
        return config;
    }
    

    您也可以通过实现表视图的 tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: 方法来实现从右向左滑动时显示删除按钮。

    这是您可以使用的一个例子:

    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Create a delete action
        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            // Perform delete action here
            // Remove the item from the data source
            [arrayList removeObjectAtIndex:indexPath.row];
            // Delete the row from the table view
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Call completion handler to dismiss the action button
            completionHandler(YES);
        }];
        // Create and return a swipe actions configuration with the delete action
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
        return config;
    }
    

    请注意,您可能还需要在表视图的代理中设置 editingStyleForRowAtIndexPath: 为 UITableViewCellEditingStyleDelete,以便当用户从左向右滑动时也显示删除按钮。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器