美文网首页
iOS tableView自定义测滑事件

iOS tableView自定义测滑事件

作者: 肖显圣 | 来源:发表于2020-11-28 10:51 被阅读0次

一般我们在使用tableView时总会出现侧滑需求问题,以下就是统计的系统侧滑和自定义侧滑,废话不多说上代码;

当然首先得允许侧滑:

//允许侧滑
- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath
{
    return YES;
}

系统自带的侧滑方法:

//2.侧滑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//3.修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//4.侧滑事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //操作
}

当然系统自带的只能满足一个按钮操作需求,多个操作方式就得使用自定义方法:如下

//自定义
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"点击了删除");

    }];
    deleteRowAction.backgroundColor = RGBCOLOR(245, 245, 245, 1);
    
    /*
    UITableViewRowAction * editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"点击了编辑");
    }];
    editRowAction.backgroundColor = RGBCOLOR(245, 245, 245, 1);

     // 将设置好的按钮放到数组中返回
    NSArray *array = @[deleteRowAction,editRowAction];
   */
    NSArray *array = @[deleteRowAction];
    return array;
}

此自定义方法与系统方法实现的效果一致;
但如果想替换删除按钮的图片 - 监听即将开启编辑状态的事件:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self configSwipeButtons];
}
//即将滑动
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self.view setNeedsLayout];
}

设置左滑出来的按钮样式

- (void)configSwipeButtons
{
    if (@available(iOS 13.0, *))
    {
      //iOS 13层级 UITableView->_UITableViewCellSwipeContainerView->UISwipeActionPullView
        for (UIView *subview in self.tableView.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] )
            {
                for (UIView *actionView in subview.subviews)
                {
                    if ([actionView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
                    {
                        actionView.backgroundColor = [UIColor clearColor];
                        if (actionView.subviews.count >= 1)
                        {
                            UIButton *deleteButton = actionView.subviews[0];
                            [self configDeleteButton:deleteButton];
                        }
                    }
                }
            }
        }
    }
    else if (@available(iOS 11.0, *))
    {
        // iOS 11层级 (Xcode 8编译): UITableView -> UITableViewWrapperView -> UISwipeActionPullView
        for (UIView *subview in self.tableView.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewWrapperView")])
            {
                for (UIView *actionView in subview.subviews)
                {
                    if ([actionView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
                    {
                        actionView.backgroundColor = [UIColor clearColor];
                        if (actionView.subviews.count >= 1)
                        {
                            UIButton *deleteButton = actionView.subviews[0];
                            [self configDeleteButton:deleteButton];
                        }
                    }
                }
            }
        }
    }
}
- (void)configDeleteButton:(UIButton*)deleteButton
{
    deleteButton.titleLabel.font = [UIFont systemFontOfSize:CS6(15)];
    [deleteButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [deleteButton setImage:[UIImage imageNamed:@"项目删除"] forState:UIControlStateNormal];
    deleteButton.layer.cornerRadius = 0;
    deleteButton.layer.masksToBounds = YES;
}

这一步我们就已经实现我们所需要的效果了,但是有个小bug,当你快速左滑/侧滑时并不能执行将要滑动的方法

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath

但是轻轻滑动却能执行,删除事件还是在那儿位置响应的,目前不清楚是什么原因,应该是系统漏洞;
后来发现苹果在ios11后推出了新的方法来方便大家自定义侧滑按钮,

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

可以发现 UIContextualAction 内置了image

UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos)
@interface UIContextualAction : NSObject

+ (instancetype)contextualActionWithStyle:(UIContextualActionStyle)style title:(nullable NSString *)title handler:(UIContextualActionHandler)handler;

@property (nonatomic, readonly) UIContextualActionStyle style;
@property (nonatomic, copy, readonly) UIContextualActionHandler handler;

@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, null_resettable) UIColor *backgroundColor; // a default background color is set from the action style
@property (nonatomic, copy, nullable) UIImage *image;

@end

经过测试,发现下面事件,在左划发生时候,始终是响应的。

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0))
{
    UIContextualAction * addAction = [UIContextualAction
                                        contextualActionWithStyle:UIContextualActionStyleDestructive
                                        title:@""
                                        handler:^(UIContextualAction * _Nonnull action,
                                                  __kindof UIView * _Nonnull sourceView,
                                                  void (^ _Nonnull completionHandler)(BOOL))
                                        
    {
        //操作
        NSLog(@"点击了删除");
        //还原
        completionHandler(true);
    }];
    addAction.image = [UIImage imageNamed:@"项目删除"];
    addAction.backgroundColor = RGBCOLOR(245, 245, 245, 1);
    
    //添加
    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[addAction]];
    
    //默认为YES,设置为NO以防止完全滑动执行第一个动作
    actions.performsFirstActionWithFullSwipe = NO;
    
    return actions;
}

完美解决。

相关文章

网友评论

      本文标题:iOS tableView自定义测滑事件

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