您的当前位置:首页正文

IOS 兼容ios11以前和ios11以后的TableView自

来源:图艺博知识网

//cell
/*自定义设置iOS8-10系统下的左滑删除按钮大小/


- (void)layoutSubviews {
    [super layoutSubviews];
    
    for (UIView * subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            subView.backgroundColor = [UIColor clearColor];//去掉默认红色背景
            
            UIButton * stopButton = subView.subviews[0];
            [stopButton setImage:[UIImage imageNamed:@"stop_btn"] forState:UIControlStateNormal];
            [stopButton setTitle:@"" forState:UIControlStateNormal];
            
            UIButton * editButton = subView.subviews[1];
            [editButton setTitle:@"" forState:UIControlStateNormal];
            
            
            [editButton setImage:[UIImage imageNamed:@"edit_btn"] forState:UIControlStateNormal];
        }
    }
}

-----------------TableViewDelegate,UITableViewDataSource-----------------
/*自定义设置iOS11系统下的左滑删除按钮大小/



#pragma mark - 设置删除代理方法
/**使用系统默认的删除按钮
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete){
 
 }
 }
 //自定义系统默认的删除按钮文字
 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
 return @"自定义按钮";
 }
 */

//自定义多个左滑菜单选项
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"停用" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
    }];
    UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"启用" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [tableView setEditing:NO animated:YES];//退出编辑模式,隐藏左滑菜单
    }];
    
    deleteAction.backgroundColor = [UIColor LineColor];
    shareAction.backgroundColor = [UIColor LineColor];
    return @[deleteAction,shareAction];
}
/**自定义设置iOS11系统下的左滑删除按钮大小*/
//开始编辑左滑删除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        for (UIView * subView in self.tableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
                subView.backgroundColor = [UIColor clearColor];//如果自定义只有一个按钮就要去掉按钮默认红色背景
                
                UIButton * stopButton = subView.subviews[0];
                [stopButton setImage:[UIImage imageNamed:@"stop_btn"] forState:UIControlStateNormal];
                [stopButton setTitle:@"" forState:UIControlStateNormal];
                
                UIButton * editButton = subView.subviews[1];
                [editButton setImage:[UIImage imageNamed:@"edit_btn"] forState:UIControlStateNormal];
                [editButton setTitle:@"" forState:UIControlStateNormal];

               
                [stopButton addTarget:self action:@selector(btnEditClick:) forControlEvents:UIControlEventTouchUpInside];
                [editButton addTarget:self action:@selector(btnStopClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
}

//结束编辑左滑删除
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

//判断是否显示左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


Top