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. 京东深圳前端研发(只有通过邮件,还有收到 ...
随机推荐
- 保持长宽比 对背景图像进行修改android:scaleType="fitXY"
关于android中ImageView的外观,即图片在其内显示出的样子,与布局文件中adjustViewBonds和scaleType属性的关系.我进行了一些探索.现跟大家共享,欢迎各位指教.分别将a ...
- web压力测试的轻量级具体做法
一:压力测试中需要掌握的几个基本概念 1:吞吐率(Requests per second) 服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数.某个并发用户 ...
- AutoCAD按坐标打印图纸
前几天公司要求按坐标打印DWG文件,中间走了不少弯路,好在已经搞定了,整理一下分享给大家,希望后来人少走弯路. 1. 设计需求: 公司的图纸用AutoCAD2010做成,通常一个项目的所有图纸都存放在 ...
- [AngularJS] Directive Definition Objects (DDO)
This function that we just set up is what's called a link function, and it's actually a very small p ...
- iOS开发——UI篇Swift篇&玩转UItableView(二)高级功能
UItableView高级功能 class UITableViewControllerAF: UIViewController, UITableViewDataSource, UITableViewD ...
- 清除SQL Server 2008中登陆时的历史记录
win7 在地址栏直接输入下面路径,删除SqlStudio.bin文件%AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell
- 判断浏览器是否IE10
项目中做打印预览时,在IE10中出现兼容性问题,需要针对IE10做特殊处理. 在网上搜了一下,有三种方法可以实现 一.特性检测:@cc_on <!--[if !IE]><!--> ...
- Eclipse快捷键 10个最有用的快捷键【转】
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- Cocos2d-X内存管理研究<一>
http://hi.baidu.com/tzkt623/item/651ca7d7a0aff6e055347f67 半夜没事干,研究内核,作为我cocos2d-x的第一篇教程.cocos ...
- 剑指 offer set 10 栈的压入、弹出序列
总结 1. 通过按位对比来判断, 没有更优的方法了