//  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. 使用sqlite的命令操作

    一:  首先进入到D:\java\android\android-sdk\platform-tools文件夹里面 二:使用adb  shell进入shell命令方式行(注意要想进入shell里面的操作 ...

  2. Linux文件时间属性

    Linux文件时间属性                                                                                         ...

  3. 使用@media实现IE hack的方法

    文章简介:众所周知,有些时候为了实现IE下的某些效果与现代浏览器一致,我们不得不使用一些hack手段来实现目的.比如说使用“\0”,“\”和“\9”来仅让IE某些版本识别,而对于现代浏览器来说,他会直 ...

  4. Oracle的trunc和dbms_random.value随机取n条数据

    今天在review项目代码的时候看到这样一个问题,有一张号码表,每次需要从这样表中随机取6个空闲的号码,也就是每次取出来的6个号码应该都会有所不同.然后我就看到了这样的SQL select   t.* ...

  5. The Kernel Newbie Corner: Kernel Debugging with proc "Sequence" Files--Part 3

    转载:https://www.linux.com/learn/linux-career-center/44184-the-kernel-newbie-corner-kernel-debugging-w ...

  6. ES6新特性以及一些规范

    1.let:使变量成为块级变量,类似于C++,java类的变量 b = 2 if (b == 2) { let c = 2; } console.log(c) // 报错,因为c是一个块级变量,只存在 ...

  7. gridview_RowCommand 获取当前行中的控件

    <asp:GridView ID="gvStorglog" runat="server" Width="100%" SkinID=&q ...

  8. .net 后台获取当前请求的设备

    检查当前发起请求的设备是手持设备还是电脑端  以便显示不同的视图 public static bool CheckIsMobile(HttpRequestBase req) { bool flag = ...

  9. Redis缓存、MemCached和.Net内部缓存的切换使用

    接口文件:IDataCache.cs using System; using System.Collections.Generic; using System.Linq; using System.T ...

  10. LocalActivityManager的内部机制

    LocalActivityManager内部机制的核心在于,它使用了主线程对象mActivityThread来装载指定的Activity.注意,这里是装载,而不是启动,这点很重要. 所谓的启动,一般是 ...