zoukankan      html  css  js  c++  java
  • 左滑功能和多选功能(在一些列表都会用到的功能)

    左滑功能

    左划删除

    1.实现UITableViewDelegate协议和代理
    2.实现左划删除功能和修改按钮文字的代理方法
    注意:此时按钮没有反应,下面第一个方法可以实现对按钮的监听事件,就可以做出操作
    /**
     *  重写这个方法,就可以实现左划删除功能
     */
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 删除模型数组第一个东西
        // 1.修改模型
        [self.wineArray removeObjectAtIndex:indexPath.row];
        // 2.刷新数据
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    /**
     *  重写实现左划删除为中文格式,可以直接设置(下面附图)
     */
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除";
    }

    自定义左划按钮

    >和上面一样,实现tableview的协议和代理
    >实现tableView对应的代理方法
    
    注意:实现了下面的方法,那么上面的修改文字功能就不实现,在ios9之后上面的第一个方法也不实现,
        左划出现按钮tableview就进入了编辑模式,所以要它退出左划就直接设置退出编辑模式
        self.tableview.editing = NO;
    /**
     *  自定义左移出现的按钮
     */
    - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView 
        editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.tableView.editing = YES;
        
        UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
         title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            /************** 删除功能 ***************/
            
            // 修改模型
            [self.wineArray removeObject:self.wineArray[indexPath.row]];
            // 刷新表格
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
        
        UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
         title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            /************ 退出编辑模式 *************/
            
            self.tableView.editing = NO;
        }];
        
        return @[action1, action2]; // 越先添加,按钮越靠近右边
    }

    多选情况

    多选

        self.tableView.allowsMultipleSelection = YES;

    编辑模式下多选

        self.tableView.allowsMultipleSelectionDuringEditing = YES;

    事例:要批量删除,然后整体删除

        self.tableview.indexPathsForSelectedRows // 可以保持选中的行
        注意:数组不能遍历的同时删除,会删错东西

    代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 编辑模式下的多选
        self.tableView.allowsMultipleSelectionDuringEditing = YES;
    }
    /**
     *  批量删除
     */
    - (IBAction)deleteAll { // 实现点击换button功能
        /**** 根据编辑模式设置可选状态 ****/
        [self.tableView setEditing:!self.tableView.editing animated:YES];
        self.deleteAllBtn.selected = self.tableView.editing;
    }
    /**
     *  删除
     */
    - (IBAction)delete {
        
        /***** 1.找出要删除的行 *****/
        NSMutableArray *deleteArray = [NSMutableArray array];
        // self.tableView.indexPathsForSelectedRows 选中的行
        NSArray *rowsArray = self.tableView.indexPathsForSelectedRows;
        for (NSIndexPath *indexPath in rowsArray) {
            [deleteArray addObject:self.wineArray[indexPath.row]];
        }
        
        /***** 2.修改模型 *****/
        [self.wineArray removeObjectsInArray:deleteArray];
        
        /***** 3.刷新表格 *****/
        [self.tableView deleteRowsAtIndexPaths:rowsArray 
            withRowAnimation:UITableViewRowAnimationAutomatic];
    }
  • 相关阅读:
    IPv4地址被用光,IPv6将接手
    杀猪盘
    大家都应该看看这个贴子,会让你心明眼亮。 注意到这些变化了吗?中国正在发生的100个变化,越往后读越震惊!
    区块链在中国怎么练?
    区块链到底是什么样的技术呢?
    2019感恩节
    人工智能、大数据、物联网、区块链,四大新科技PK,你更看好谁?
    vue遇见的问题(2)---imported multiple times(转载)
    drf-序列化器的理解
    Django rest_framework序列化many=True参数解释
  • 原文地址:https://www.cnblogs.com/LongLJ/p/5002044.html
Copyright © 2011-2022 走看看