iOS:cell 编辑状态时的按钮

灵活应用cell编辑状态的按钮

//宏定义
#define SYSTEM_VERSION_GRETER_THAN(v)   ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

#define SYSTEM_VERSION_LESS_THAN(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath{
    self.editIndexPath = indexPath;
    [self.view setNeedsLayout];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self configSwipeButtons];
}

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    //cell - 删除
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        // 点击删除时 do something
        /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */
        [self.selectedPhotos removeObjectAtIndex:indexPath.row];
        [self.selectedAssets removeObjectAtIndex:indexPath.row];
        
        /** 2. TableView中 删除一个cell. */
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    }];
    
    [rowAction setTitle:@"删除"];
    NSArray *arr = @[rowAction];
    return arr;
}

- (void)configSwipeButtons{
    
    // 获取选项按钮的reference
    if (SYSTEM_VERSION_GRETER_THAN(@"11.0")){
        // iOS 11层级 : UITableView -> UISwipeActionPullView
        for (UIView *subview in self.tableView.subviews){
            //这里写大于等于2是因为我这里需要两个action
            
            if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]){
                // 和iOS 10的按钮顺序相反
                UIButton*deleteButton = subview.subviews[0];
                [deleteButton setImage:[UIImage imageNamed:@"cell_delete1"] forState:UIControlStateNormal];
                [deleteButton setTitle:@"" forState:UIControlStateNormal];
             }
         }
    }else{
        // iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
        UploadMaterCell *tableCell = [self.tableView cellForRowAtIndexPath:self.editIndexPath];
        for(UIView *subview in tableCell.subviews){
            
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]){
                
                UIButton*deleteButton = subview.subviews[0];
                [deleteButton setImage:[UIImage imageNamed:@"cell_delete1"] forState:UIControlStateNormal];
                [deleteButton setTitle:@"" forState:UIControlStateNormal];
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_15289761/article/details/106826120
今日推荐