cell_滑动

本文介绍如何在iOS应用中为UITableView配置滑动操作,包括禁用、启用和删除功能,通过UIKit框架的UIContextualAction和UISwipeActionsConfiguration实现自定义交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
//iOS_11.0
-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak typeof(self) weakSelf = self;
    
    __block ZLJInventDataItem *inventModel = self.inventArray[indexPath.row];
    UIContextualAction    *other;
    //禁用状态
    if (inventModel.useState == 1) {
        //需要使用启用功能
        other = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
                                                        title:@"启用"
                                                      handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                                                          [weakSelf updateShareReocrdWithRecordId:inventModel.kId
                                                                                             type:@"0"
                                                                                        startTime:inventModel.startTime 
                                                                                        doSuccess:^(BOOL success) {
                                                                                            
                                                                                            if (success) {
                                                                                                inventModel.useState = 0;
                                                                                                [weakSelf.recordTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                                                                            }
                                                                                        }];
                                                          completionHandler(NO);
                                                      }];
        
    }else if(inventModel.useState == 0){//启用状态
        //需要使用禁用功能
        other = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
                                                        title:@"禁用"
                                                      handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                                                          [weakSelf updateShareReocrdWithRecordId:inventModel.kId
                                                                                             type:@"1"
                                                                                        startTime:inventModel.startTime 
                                                                                        doSuccess:^(BOOL success) {
                                                                                            
                                                                                            if (success) {
                                                                                                inventModel.useState = 1;
                                                                                                [weakSelf.recordTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                                                                            }
                                                                                        }];
                                                          completionHandler(NO);
                                                      }];
    }
    UIContextualAction    *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
                                                                            title:@"删除"
                                                                          handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
                                                                              [weakSelf updateShareReocrdWithRecordId:inventModel.kId
                                                                                                                 type:@"2"
                                                                                                            startTime:inventModel.startTime 
                                                                                                            doSuccess:^(BOOL success) {
                                                                                                                if (success) {
                                                                                                                    [weakSelf.inventArray removeObjectAtIndex:indexPath.row];
                                                                                                                    [weakSelf.recordTableView reloadData];
                                                                                                                }
                                                                                                            }];
                                                                              completionHandler(NO);
                                                                          }];
    
    other.backgroundColor = [UIColor lightGrayColor];
    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[delete,other]];
    actions.performsFirstActionWithFullSwipe = NO;
    return actions;
}
*/
//iOS_8.0
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal 
                                                                         title:@"删除" 
                                                                       handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
    
                                                                       }];
    rowAction.backgroundColor = [UIColor redColor];
    UITableViewRowAction *rowaction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal 
                                                                         title:@"禁用" 
                                                                       handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
                                                                       }];
    rowaction.backgroundColor = [UIColor grayColor];
    NSArray *arr = @[rowAction,rowaction];
    return arr;
}

 

转载于:https://www.cnblogs.com/tom2015010203/p/9927958.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值