允许左滑删除
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
// 替换删除按钮
[self configSwipeButtons];
}
// 是否允许对cell进行滑动操作
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定义按钮为删除按钮
return UITableViewCellEditingStyleDelete;
}
-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (@available(iOS 11.0, *)) {
//删除
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL))
{
// 执行删除的操作
// 。。。。。你的代码
[self configSwipeButtons];
}];
deleteAction.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
NSArray<UIContextualAction*> *contextualAction;
contextualAction = @[deleteAction];
UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:contextualAction];
// 禁止侧滑无线拉伸
actions.performsFirstActionWithFullSwipe = NO;
[self.view setNeedsLayout];
return actions;
}else{
[self.view setNeedsLayout];
return nil;
}
}
/*
-(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;
}
}
}
}
}