zoukankan      html  css  js  c++  java
  • UITableview 多行删除

    //  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];
        }
    }

  • 相关阅读:
    网上流行的学生选课相关的50个常用sql语句
    the interview questions of sql server
    ASP.NET 之 Chart Control for .Net Framework
    SQL Server之纵表与横表互转
    ASP.NET控件之RadioButtonList
    SQL Server通过钉钉机器人直接发送消息
    使用AMO对象,更改款属性名称
    常用MDX函数
    Excel 插入图片
    Sql Server 2008查询数据库中各表记录行数
  • 原文地址:https://www.cnblogs.com/sayimba/p/5661142.html
Copyright © 2011-2022 走看看