UI

ios UITableView 自定义左滑删除按钮

2020-08-05  本文已影响0人  songjk

允许左滑删除

       // 是否允许对cell进行滑动操作
    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 定义左滑功能为删除
        UITableViewRowAction * actionDeleate = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"\b" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            // 删除的具体操作
        }];
        actionDeleate.backgroundColor = [UIColor whiteColor];
        return @[actionDeleate];
    }

定义左滑出来的删除按钮 在控制器 viewDidLayoutSubviews中执行

        - (void)configSwipeButtons
    {
          // iOS 11层级 : UITableView -> UISwipeActionPullView
          for (UIView *subview in self.tableView.subviews)
          {
               // ios 11以下
               if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 1)
               {
                    UIButton*readButton = subview.subviews[0];
                    [readButton setImage:[UIImage imageNamed:@"plan_delete"] forState:UIControlStateNormal];
                    break;
               }
                // ios 13
               else if ([subview isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subview.subviews count] >= 1) {
                   for (UIView *subview0 in subview.subviews){
                       if ([subview0 isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview0.subviews count] >= 1){
                           UIButton *deleteButton = subview0.subviews[0];
                           [deleteButton setImage:[UIImage imageNamed:@"plan_delete"] forState:UIControlStateNormal];
                           break;
                       }
                   }
               }

          }
    }
上一篇 下一篇

猜你喜欢

热点阅读