UI3_UITableViewDelete(多选)
// 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(多选)的更多相关文章
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- Jquery的点击事件,三句代码完成全选事件
先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- React使用antd Table生成层级多选组件
一.需求 用户对不同的应用需要有不同的权限,用户一般和角色关联在一起,新建角色的时候会选择该角色对应的应用,然后对应用分配权限.于是写了一种实现的方式.首先应用是一个二级树,一级表示的是应用分组,二级 ...
- 关于 devbridge-autocomplete 插件多选操作的实现方法
目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件. 我最终选择了 devbrid ...
- 企业做数据缓存是使用Memcached还是选Redis?
企业是使用Memcached还是选Redis? 在构建一款现代且由数据库驱动的Web应用程序并希望使其拥有更为出色的性能表现时,这个问题总会时不时出现.并给每一位开发人员带来困扰.在考虑对应用程序的性 ...
- css3更改input单选和多选的样式
在项目开发中我们经常会遇到需要更改input单选和多选样式的情况,今天就给大家介绍一种简单改变input单选和多选样式的办法. 在这之前先简单介绍一下:before伪类 :before 选择器向选定的 ...
- jquery实现下拉框多选
一.说明 本文是利用EasyUI实现下拉框多选功能,在ComboxTree其原有的基础上对样式进行了改进,样式表已上传demo,代码如下 二.代码 <!DOCTYPE html PUBLIC & ...
- jquery.multiselect 多选下拉框实现
第一步:链接下列文件,如果没有,到此网页下载 https://github.com/ehynds/jquery-ui-multiselect-widget,此插件基于jquery ,所以jquery的 ...
- 前端如何正确选择offer,到底选哪个?
文章背景:来自于一次线上交流,当时回答感觉比较粗糙,做个阶段性的总结,也分享给其它朋友. 当时的题目是,共2个offer,如何选择: 1. 美团外卖前端 2. 京东深圳前端研发(只有通过邮件,还有收到 ...
随机推荐
- [Angular2 Router] Build Angular 2 Navigation with routerLink
Angular 2 navigation is configured using the routerLink directive. The routerLink directive behaves ...
- 一天掌握Android JNI本地编程 快速入门
一.JNI(Java Native Interface) 1.什么是JNI: JNI(Java Native Interface):java本地开发接口 ...
- iOS开发——语法OC篇&BOOL / bool / Boolean / NSCFBoolean
Name Typedef Header True Value False Value BOOL signed char objc.h YES NO bool _Bool (int) stdbool.h ...
- cxx-generator JS绑定工具
第一部分:配置安装环境 cxx-generator是由Zynga工程师贡献的C++代码绑定到js工具.用于将cocos2d-x 的c++代码,生成相应的js绑定代码(由c++写成),然后将这些函数注册 ...
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- python实现二叉树遍历算法
说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...
- 简要地写出一个.NET Remoting的示例
在VS 2008中添加新的类库项目,并命名为NetRmClass,将所属解决方案命名为NetRm,勾选“创建解决方案的目录”.这样,NetRmClass类库项目目录即属于NetRm解决方案,并可以继续 ...
- nodejs的mysql模块学习(六)连接池的创建和使用
介绍 在 软件工程 , 连接池 是一个 高速缓存 的 数据库连接 维持,使得连接可以当需要将来向数据库请求重复使用. [ 来源请求 ] 连接池用于提高数据库上执行命令的性能. 打开并保持每个用户的数据 ...
- 免费公测:RDS只读实例
免费公测:RDS只读实例 简要介绍 在对数据库有少量写请求,但有大量的读请求的应用场景下,单个实例可能无法抵抗读取压力, 甚至对主流程业务产生影响.为了实现读取能力的弹性扩展,分担数据库压力,阿里 ...
- iis8 默认不支持svc解决方法
最近在IIS8中发布WCF服务应用时,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF服务的Managed Handler,按照以下步骤添加后,IIS8即支持WCF服务. 1. 首 ...