iOS:cell编辑状态时的按钮

本文探讨了在iOS开发中如何在UITableViewCell的编辑状态下添加和处理按钮,主要涉及Objective-C编程语言,利用Xcode进行开发。通过实例解析,展示了在cell编辑模式下灵活配置和交互按钮的方法。
摘要由CSDN通过智能技术生成

灵活应用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];
            }
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值