//  AppDelegate.m
// UI3_UITableViewDelete(多选)
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible]; return YES;
}
//
// ViewController.h
// UI3_UITableViewDelete(多选)
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @end //
// ViewController.m
// UI3_UITableViewDelete(多选)
//
// Created by zhangxueming on 15/7/14.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
{
UITableView *_tableView;
//数据源
NSMutableArray *_dataList;
//要删除的数据
NSMutableArray *_removeList;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self creatDataSource];
[self creatUI];
} //创建数据源
- (void)creatDataSource
{
_dataList = [NSMutableArray array];
_removeList = [NSMutableArray array];
NSMutableArray *boys = [NSMutableArray array];
for (int i=0; i<20; i++) {
NSString *name = [NSString stringWithFormat:@"boy:%d", i+1];
[boys addObject:name];
}
[_dataList addObject:boys]; NSMutableArray *grils = [NSMutableArray array];
for (int i= 0; i<10; i++) {
NSString *name = [NSString stringWithFormat:@"gril:%d", i+1];
[grils addObject:name];
}
[_dataList addObject:grils];
} //创建UI - (void)creatUI
{
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; //设置代理
_tableView.delegate = self;
_tableView.dataSource = self; //设置尾视图
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 44)];
footerView.backgroundColor = [UIColor cyanColor];
_tableView.tableFooterView = footerView;
NSLog(@"view = %@", _tableView.tableFooterView);
//设置头视图
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 44)];
headerView.backgroundColor = [UIColor yellowColor];
_tableView.tableHeaderView = headerView; //添加Btn UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame =CGRectMake(50, 0, footerView.frame.size.width-100,44);
[btn setTitle:@"remove" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(removeData) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:btn]; //设置编辑按钮
self.navigationItem.leftBarButtonItem = self.editButtonItem; [self.view addSubview:_tableView]; } //进入编辑状态 - (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:YES];
[_tableView setEditing:editing animated:YES];
} //Editing - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//同时设置insert 及 delete 自动进入多选状态
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
} //删除数据
- (void)removeData
{
for (int i=0; i<2; i++) {
NSArray *array = [[_dataList objectAtIndex:i] copy];
NSInteger rows = array.count;
for (int j=0; j<rows; j++) {
NSString *obj = [array objectAtIndex:j];
if ([_removeList containsObject:obj]) {
[[_dataList objectAtIndex:i] removeObject:obj];
}
}
}
[_tableView reloadData];
} #pragma mark ---UITableView--- //返回分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dataList.count;
} //返回每个分区有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[_dataList objectAtIndex:section] count];
} //返回UITableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
} //选中状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ if (tableView.editing) {
NSString *obj = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if (![_removeList containsObject: obj])
{
[_removeList addObject:obj];
}
}
} //结束选中状态
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.editing) {
NSString *obj = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if ([_removeList containsObject:obj]) {
[_removeList removeObject:obj];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UI3_UITableViewDelete(多选)的更多相关文章

  1. .net点选验证码实现思路分享

    哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...

  2. Jquery的点击事件,三句代码完成全选事件

    先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  3. React使用antd Table生成层级多选组件

    一.需求 用户对不同的应用需要有不同的权限,用户一般和角色关联在一起,新建角色的时候会选择该角色对应的应用,然后对应用分配权限.于是写了一种实现的方式.首先应用是一个二级树,一级表示的是应用分组,二级 ...

  4. 关于 devbridge-autocomplete 插件多选操作的实现方法

    目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件. 我最终选择了 devbrid ...

  5. 企业做数据缓存是使用Memcached还是选Redis?

    企业是使用Memcached还是选Redis? 在构建一款现代且由数据库驱动的Web应用程序并希望使其拥有更为出色的性能表现时,这个问题总会时不时出现.并给每一位开发人员带来困扰.在考虑对应用程序的性 ...

  6. css3更改input单选和多选的样式

    在项目开发中我们经常会遇到需要更改input单选和多选样式的情况,今天就给大家介绍一种简单改变input单选和多选样式的办法. 在这之前先简单介绍一下:before伪类 :before 选择器向选定的 ...

  7. jquery实现下拉框多选

    一.说明 本文是利用EasyUI实现下拉框多选功能,在ComboxTree其原有的基础上对样式进行了改进,样式表已上传demo,代码如下 二.代码 <!DOCTYPE html PUBLIC & ...

  8. jquery.multiselect 多选下拉框实现

    第一步:链接下列文件,如果没有,到此网页下载 https://github.com/ehynds/jquery-ui-multiselect-widget,此插件基于jquery ,所以jquery的 ...

  9. 前端如何正确选择offer,到底选哪个?

    文章背景:来自于一次线上交流,当时回答感觉比较粗糙,做个阶段性的总结,也分享给其它朋友. 当时的题目是,共2个offer,如何选择: 1. 美团外卖前端 2. 京东深圳前端研发(只有通过邮件,还有收到 ...

随机推荐

  1. Linux makefile 教程 很具体,且易懂

    近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了下面这篇文章.通俗易懂.然后把它贴出 ...

  2. SharedObject使用:在FluorineFx.net与Flex中使用共享对象维护在线用户列表实例【转】

    一.添加一个新的FluorineFx的服务类项目OnLineService,删除原有的Sample.cs,并添加一个用户类定义与一个ApplicationAdpater类:如下: /*-- User. ...

  3. iOS开发——动画OC篇&知识点总结

    图层与动画知识点总结 1.Core Animation 非娱乐类的软件都会用到的动画,操作简单. 2.Quartz 2D绘图 是一个2D绘图引擎. (1) 绘图Context是一个绘图的目标对象,定义 ...

  4. iOS开发——UI篇Swift篇&UIScrollView

    UIScrollView //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControlle ...

  5. iOS开发——UI篇Swift篇&UISlider

    UISlider override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString // Do any a ...

  6. oc-29-可变数组

    /** 数组长度不固定,可以随便往里面添加或者删除元素. 1.创建数组 NSMutableArray *arrayM = [NSMutableArray array] 2.给数组添加元素(只能是OC对 ...

  7. HANDLER命令与实现

    MySQL“自古以来”都有一个神秘的HANDLER命令,而此命令非SQL标准语法,可以降低优化器对于SQL语句的解析与优化开销,从而提升查询性能.看到这里,可能有小伙伴不淡定了,这么好的东西为啥没广泛 ...

  8. linxu,window系统

    window下:net start VMwareHostdVMAuthdServiceVMUSBArbService"VMware NAT Service"VMnetDHCP #启 ...

  9. 解决faststone capture在台式机上录制屏幕视频没有声音的问题

    点击屏幕录像机: 在弹出的屏幕录像机窗口中选择录制音频,然后点击选项按钮: 在弹出的选项按钮中选择视频选项,将所有的多选框都选中,注:最后一个选项录制windows XP的工具提示最好也选上,我发现如 ...

  10. Solr DIH dataconfig配置

    1. 配置文件data-config.xml定义了数据库的基本配置,以及导出数据的映射规则,即导出数据库表中对应哪些字段的值,以及对特定字段的值做如何处理 </pre><p>& ...