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

时间: 2024-12-20 19:51:16

UITableview 多行删除的相关文章

IOS7 UITableView一行滑动删除后 被删除行的下一行的点击事件将被忽略解决办法

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     returnUITableViewCellEdit

Snail—UI学习之表视图TableView多行删除

这次实现的功能是多行cell进行删除 代码是在上一次的基础上进行修改的 有的代码删除重写 有的方法只是加了一些逻辑判断 // // WJJRootViewController.m // blog_UITableView // // Created by Snail on 15-7-30. // Copyright (c) 2015年 Snail. All rights reserved. // #import "WJJRootViewController.h" @interface W

uitableview多行选择

1 interface ViewController () <UITableViewDataSource, UITableViewDelegate> 2 3 @property (weak, nonatomic) IBOutlet UITableView *tableView; 4 5 @end 6 7 @implementation ViewController 8 9 10 11 - (void)viewDidLoad 12 13 { 14 15 [super viewDidLoad];

利用命令行删除Android系统自带应用的方法

一般来说,手机厂家都会在手机中内置许多应用,而这些应用是使用一般的应用程序管理无法删除的.当然,现在有一些APP,如360和豌豆荚,在获取了系统的root权限之后是可以删除自带应用的.但是如果我不想让一个app来获取我的root权限呢?有没有方便.快捷的方法呢? 当然有,那就是利用shell命令.当然,首先要安装当前手机的驱动程序,否则无法进行调试. 在如何删除Android系统中的内置应用一文中作者也介绍了利用命令行删除系统应用的方法,但是个人感觉太麻烦了,其实有更简单的方法,只要三步即可:a

UITableView 左划删除

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { returnYES; } -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPat

asp.net 行删除原理(浏览器端请求,服务器端响应)

1.asp.net中数据绑定控件中行按钮和行超链接实现方式不同:ListView中Button,HyperLink两种行删除方式,按钮方式是将行的id通过表单提交到服务器:行超链接的方式是通过超链接的url通过get的方式提交给处理页面,超链接的方式由于没有提交所有的表单信息,因此很多服务端控件的高级用法用不了. 2.超链接的因为没有向服务器提交ViewState等隐藏字段,所以处理时IspostBack是false:而按钮提交的表单,所以ispostback是true.可以在超链接的href中

php处理文件,一行一行的读取,并且把没用的行删除。

今天做sitemap.xml.找了个国外的网站,http://www.freesitemapgenerator.com/这个可以生成5000条数据,以前找那个只能生成500条.但是,生成的xml标签中有些是没有用的,如图: 于是想到了php处理文件,一行一行的读取,并且把没用的行删除. 代码如下: <?php  set_time_limit(0); $file=fopen('sitemap.xml','r'); while (!feof($file)){   $line = fgets($fil

VIM中的复制剪切移动多行删除

1.复制 使用yy复制一行 使用 行数n+yy 复制n行 使用p对复制的行进行粘贴 2.剪切 使用dd剪切一行 使用 行数n+dd 剪切n行 使用p对剪切的行进行粘贴 3.移动 Vim用数字+G来移动行,比如你要移动到342行,那就是342G 4.多行删除 首先在命令模式下,输入":set nu"显示行号:通过行号确定你要删除的行:命令输入":32,65d",回车键,32-65行就被删除了

iOS UITableView划动删除的实现

标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/634587 从七八月前对苹果一无所知,到现在手持iphone,ipad,itouch有三个线上成熟app并熟练开发ios应用.一路走来一直站在前辈的肩膀上不断进步.如今生活工作稳定是时候将一直以来的一些心得整理出来了.想来想去决定先说说UITab