//  RootViewController.m

#import "RootViewController.h"
#import "NextViewController.h"
@interface RootViewController ()
{
    NSMutableArray * dataSource;//数据源
    NSMutableArray * removeArr;//存放删除的所有元素
    
    UITableView * table;
    BOOL isEditing;//标识表格的编辑状态
    NextViewController * next;
}
@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"表格视图";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;//编辑按钮
    
    //初始化数据源
    dataSource = [[NSMutableArray alloc]init];
    for(int i = 0;i<10;i++)
    {
        NSString * string = [NSString stringWithFormat:@"测试数据%d",i+1];
        [dataSource addObject:string];
    }
    //初始化表格视图
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 480 - 64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    
    //设置当前表格的编辑状态
    isEditing = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //为表格添加额外的视图控件
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 300, 44);
    //btn按钮的frame的有效值为宽和高
    //为表格视图添加额外控件必须借助表格的两个属性之一 tableFooterView或者tableHeaderView 将额外的控件添加在表格的底部或者顶部
    //tableFooterView和tableHeaderView这两个位置的视图高度为44像素
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    //将按钮添加到表格的底部
    table.tableFooterView = btn;
    
    //初始化子视图控制器对象
    next = [[NextViewController alloc]init];
    //删除数组进行初始化操作
    removeArr = [[NSMutableArray alloc]init];
}
-(void)pressBtn:(id)sender
{
    
    if(isEditing)//处于编辑状态 才进行删除
    {
        //<1>先移除数据源中与删除数组中相同的元素信息
        [dataSource removeObjectsInArray:removeArr];
        //<2>清空删除数组中的内容
        //如果不清空删除数组中的内容 下一次删除操作会在删除数组的内容基础上继续追加 那么第一步的移除操作一定会程序崩溃
        [removeArr removeAllObjects];
        
        //<3> -------  重要 -------
        // 刷新表格
        // 否则数据源信息和表格上显示的信息不一致
        [table reloadData];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * string = @"str";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
    }
    cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
    return cell;
}
//开启表格的编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:YES];
    isEditing = !isEditing;
    //判断表格处于非编辑状态的时候 (避免选中单元格以后直接设置表格为非编辑状态 再次编辑表格的时候将上一次选中的单元格一同删除)
    if(!isEditing)
    {
        [removeArr removeAllObjects];
    }
    [table setEditing:isEditing animated:YES];
}
//设置单元格的编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
    //同时为单元格设置编辑样式为删除和添加样式 那么该单元格就变成多选样式
}
//单元格选中时调用该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通过判断表格是否处于编辑状态 来决定单元格的状态
    if(isEditing)
    {
        [removeArr addObject:[dataSource objectAtIndex:indexPath.row]];
    }
    else
    {
        //处于非编辑状态就可以进行界面跳转操作
        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString * text = cell.textLabel.text;
        //获取点击单元格上的图片
        //UIImage * image = cell.imageView.image
        next.navigationItem.title = text;
        [self.navigationController pushViewController:next animated:YES];
    }
}
//反选中方法---选中的单元格再次点击就处于非选中状态
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取反选中单元格中的内容
    NSString * string = [dataSource objectAtIndex:indexPath.row];
    
    //判断删除数组是否存在反选中单元格的内容
    if([removeArr containsObject:string])
    {
        [removeArr removeObject:string];
    }
}

UITableview 多行删除的更多相关文章

  1. VI中的多行删除与复制

    VI中的多行删除与复制 法一: 单行删除,:(待删除行)d 多行删除 ,:,10d 法二: 光标所在行,dd 光标所在行以下的N行,Ndd 方法1: 光标放到第6行, 输入:2yy 光标放到第9行, ...

  2. 用bat批处理程序通过DOS命令行删除所有的空文件夹

    用过gothub或者码云的同学都知道,不包含任何文件的空文件夹上传提交时不被允许的.当然你可以在空文件下创建.keep文件(或.gitkeep文件),然后就可以上传了. 但是如果空文件夹比较多,并且我 ...

  3. vim常用命令之多行注释和多行删除

    vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率.   1.多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   ...

  4. UITableView划动删除的实现

    对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...

  5. ios UITableView多选删除

    第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...

  6. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  7. vim中多行注释和多行删除命令

      1.多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   2. 在行首使用上下键选择需要注释的多行;   3. 按下键盘(大写)“I”键,进入插入 ...

  8. VI中的多行删除与复制(转)

    VI中的多行删除与复制 法一: 单行删除,:(待删除行)d 多行删除 ,:,10d 法二: 光标所在行,dd 光标所在行以下的N行,Ndd 方法1: 光标放到第6行, 输入:2yy 光标放到第9行, ...

  9. jquery datatable 多行(单行)选择(select),行获取/行删除

    jquery datatable 多行(单行)选择(select),行获取/行删除 代码展示 // 示例数据源 var dataSet = [ ['Tasman','Internet Explorer ...

随机推荐

  1. cf158B(水题)

    题意:1辆出租车可以坐4人,已知k组人每组ki(ki<=4)人去坐车,要求同组人坐同一辆车,求最少需多少辆车.. 4人组的单独算,1人组和3人组一起,如1多余再将1和2匹配即可.... 代码如下 ...

  2. dbca no protocol support

    http://blog.itpub.net/26937943/viewspace-1325094/

  3. [编辑器] Tab转换成空格

    Notepad++: 设置 -> 首选项 -> 制表符设置 怎样设置EditPlus中Tab用空格替换http://jingyan.baidu.com/article/63f236280b ...

  4. hdu 3507 斜率dp

    不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...

  5. HDU 4345 Permutation dp

    Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...

  6. mysql数据库版本引发的问题

    改前: 改后:

  7. Linux常用命令_(网络管理)

    网络信息:hostname.netstat.route.ifconfig网络配置:netconfig网络测试:ping hostname–查看主机名称ifconfig–查看和设置网络配置–ifconf ...

  8. Xamarin.iOS项目编译提示Could not AOT the assembly

    Xamarin.iOS项目编译提示Could not AOT the assembly 错误信息:Could not AOT the assembly **************.dll 这个错误是 ...

  9. .net 日期格式转换

    DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25dt.ToFileTime().ToString();//127756416 ...

  10. API分析——Jquery UI Dialog

    1.阅读API文档的一般方法? 通常地, API由三部分构成:属性.方法.事件. 属性表示参数配置,作为一个组件的微调,或者功能的开启与关闭: 方法表示组件能够发生的动作,或者组件的状态监测: 事件表 ...