• 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)


    1.左滑动出现多个按钮

    /**
     *  只要实现了这个方法,左滑出现按钮的功能就有了
     (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES)
     */
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    }
    
    /**
     *  左滑cell时出现什么按钮
     */
    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    
            // 收回左滑出现的按钮(退出编辑模式)
            tableView.editing = NO;
        }];
    
        UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            [self.wineArray removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    
        return @[action1, action0];
    }

    2.允许多选

    // 编辑模式的时候可以多选
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    // 进入编辑模式
    [self.tableView setEditing:YES animated:YES];
    
    // 获得选中的所有行
    self.tableView.indexPathsForSelectedRows;

    3.左滑出现删除按钮

    /**
     *  只要实现了这个方法,左滑出现Delete按钮的功能就有了
     *  点击了“左滑出现的Delete按钮”会调用这个方法
     */
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 删除模型
        [self.wineArray removeObjectAtIndex:indexPath.row];
    
        // 刷新
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    
    /**
     *  修改Delete按钮文字为“删除”
     */
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除";
    }


    4.指定行刷新

    NSArray *indexPaths = @[
                            [NSIndexPath indexPathForRow:0 inSection:0],
                            [NSIndexPath indexPathForRow:1 inSection:0]
                            ];
    [self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
  • 相关阅读:
    Oracle与MySQL的转化差异
    iOS 创建静态库文件时去掉当中的Symbols
    hdu4336 Card Collector 状态压缩dp
    随机森林——Random Forests
    OpenCV码源笔记——Decision Tree决策树
    海明距离hamming distance
    学习OpenCV——Surf简化版
    学习OpenCV——用OpenCv画漫画
    学习OpenCV——ORB简化版&Location加速版
    学习OpenCV——hand tracking手势跟踪
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5793474.html
Copyright © 2020-2023  润新知