美文网首页
iOS cell 编辑状态时的按钮

iOS cell 编辑状态时的按钮

作者: 零粹 | 来源:发表于2020-01-08 14:14 被阅读0次

灵活应用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];
            }
        }
    }
}

相关文章

网友评论

      本文标题:iOS cell 编辑状态时的按钮

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