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,以便当用户从左向右滑动时也显示删除按钮。

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?