美文网首页iOS Developer
iOS UITableView 左滑显示菜单

iOS UITableView 左滑显示菜单

作者: 34码的小孩子 | 来源:发表于2018-01-31 16:50 被阅读1216次

    默认的侧滑

    实现以下方法,则可以得到默认的侧滑删除。并且在该方法处理点击事件。

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

    得到以下效果:


    default delete.png

    修改标题

    如果想要改变按钮的问题,重写以下方法设置即可:

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
        return @"返回";
    }
    
    backTitle.png

    侧滑显示多个按钮

    在iOS 8 之后,可以实现以下方法,可以得到多个按钮,每个按钮都是固定的宽度,设置太多会超出屏幕。而且,在block中处理不同按钮的点击事件。

    - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 收回侧滑
        [tableView setEditing:NO animated:YES];
    }];
    
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 删除cell: 必须要先删除数据源,才能删除cell
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    return @[deleteAction, editAction, deleteAction, editAction];
    }
    
    multiTitle.png
    editAction.backgroundColor = [UIColor yellowColor];
    

    其中,如果设置了UITableViewRowAction的背景颜色,则显示用户设置的。否则显示系统默认的,Destructive是红色,Normal 是浅灰色。

    按钮显示图标

    有一个取巧的方法,可以用平铺的图片设置成UITableViewRowAction的backgroundColor, 但是这个图标需要大一点,不然就会被平铺了。

    UIImage *image = [UIImage imageNamed:@"account_item_me"];
    editAction.backgroundColor = [UIColor colorWithPatternImage:image];
    
    bigImage.png
    smallImage.png

    iOS 11 之后,新增了两个方法, 可以直接设置图标。trailingSwipe是左滑,显示右侧菜单。leadingSwipe是又滑,显示左侧菜单。

    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available (iOS 11, *)) {
        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            if (self.dataArray.count > indexPath.row) {
                [self.dataArray removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
    
            completionHandler(YES);
        }];
    
        deleteAction.image = [UIImage imageNamed:@"public_system_success"];
    
        UIContextualAction *backAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            // 不做任何事
            [tableView setEditing:NO animated:YES];
            completionHandler(YES);
        }];
        backAction.image = [UIImage imageNamed:@"public_system_success"];
    
        UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, backAction]];
    
        return configuration;
    }
    
    return nil;
    }
    

    但是不知道哪里出错了,没有正确地显示图标,而是出现了白色圆圈。其实,应该是显示跟Cell左侧一样的图标才对。如果图片太大的话,还会将其他的菜单给覆盖了。


    strangeWhiteImage.png

    右滑,显示左侧的菜单,跟左滑的一样。

    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *editAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        // 不做任何事,只是收回侧滑
        [tableView setEditing:NO animated:YES];
        completionHandler(YES);
    }];
    
    editAction.backgroundColor = [UIColor greenColor];
    
    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[editAction]];
    
    return configuration;
    }
    
    rightSwipe.png

    相关文章

      网友评论

        本文标题:iOS UITableView 左滑显示菜单

        本文链接:https://www.haomeiwen.com/subject/mhflzxtx.html