IOS开发学习笔记029-反选、全选、删除按钮的实现
还是在上一个程序的基础上进行修改
1、反选按钮
2、全选按钮
3、删除按钮
4、其他代码优化
1、反选按钮
反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_deleteShops数组
添加一个 UIBarButtonItem 按钮,绑定响应事件.
代码如下
// 反选
- (void)unSelected
{
// 1、记录shops数组的长度和_deleteShops的长度
NSInteger shopsCount = _shops.count;
NSInteger deleteCount = _deleteShops.count; // 2、将数据全部添加到临时数组中,有先后顺序:shops在前,deleteshop在后
NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops];
[tmp addObjectsFromArray:_deleteShops]; // 3、添加数据到_deleteShops数组,取出前一部分
for (NSInteger i = ; i < shopsCount ;i ++)
{
Shop *s = [tmp objectAtIndex:i];
// 添加数据到_deleteShops数组
[_deleteShops addObject:s]; }
// 4、将取消选中的按钮从_deleteShops数组中移除数组范围(shopsCount,)后一部分,
for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++)
{
Shop *s = [tmp objectAtIndex:i];
[_deleteShops removeObject:s]; } // 5、更新表格
[_tableView reloadData];
}
2、全选\全不选按钮
全选\全不选按钮的实现主要在_deleteShops数组的数据进行增减
// 全选\全不选按钮
- (void)selectAll
{
// 1、如果一样就清空deleteShop数组
if(_deleteShops.count == _shops.count)
{
[_deleteShops removeAllObjects];
}
// 2、否则就将shops数组中数据添加到deleteshops数组中
else
{
// 先清空deleteshop数组
[_deleteShops removeAllObjects];
// 再添加数据
for (NSInteger i = ; i < _shops.count ;i ++)
{
Shop *s = [_shops objectAtIndex:i];
// 添加数据到_deleteShops数组
[_deleteShops addObject:s]; }
}
// 3、更新表格
[_tableView reloadData];
}
3、删除按钮
_deleteShops数组中保存的就是要删除的数据,直接从_shops数组中对数据进行删除就行
// 删除选中行
-(void)remove
{
// 1、删除行数据
[_shops removeObjectsInArray:_deleteShops];
// 2、删除_deleteShops数组
[_deleteShops removeAllObjects];
// 3、更新表格
[self.tableView reloadData];
}
4、其他代码实现
在上述按钮按下的过程中会有几个控件的状态改变,删除按钮状态、选中行数量(lable标签)的状态、全选按钮状态、反选按钮状态。每次都会产生数据的更改,所以每次都需要对数据界面进行刷新。
所以把这些状态的改变放到方法numberOfRowsInSection中
// 设置行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 因为每次选中这两个值都会同时改变,所以放在这里会更好,可以省去很多代码
// 更新行时判断选中cell个数显示方式,每次改变都会调用
_textlable.text = (_deleteShops.count == ) ? @"淘宝" : [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
// 删除按钮状态
_buttonDelete.enabled = (_deleteShops.count == ) ? NO : YES;
// 反选按钮状态
_unSelectBtn.enabled = (_shops.count == ) ? NO : YES;
// 全选按钮状态
_selectAllBtn.enabled = (_shops.count == ) ? NO : YES;
return _shops.count; }
效果如下:

主要代码
//
// SLQViewController.h
// UITableView-淘宝
//
// Created by Christian on 15/5/18.
// Copyright (c) 2015年 slq. All rights reserved.
// #import <UIKit/UIKit.h> @interface SLQViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *textlable; // lable标签
@property (weak, nonatomic) IBOutlet UIBarButtonItem *buttonDelete; // 删除按钮
- (IBAction)remove; // 删除事件
- (IBAction)unSelected; // 反选事件
- (IBAction)selectAll; // 全选
@property (weak, nonatomic) IBOutlet UITableView *tableView; // tableView
@property (weak, nonatomic) IBOutlet UIBarButtonItem *unSelectBtn; // 反选按钮
@property (weak, nonatomic) IBOutlet UIBarButtonItem *selectAllBtn; // 全选按钮 @end
//
// SLQViewController.m
// UITableView-淘宝
//
// Created by Christian on 15/5/18.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "SLQViewController.h"
#import "Shop.h"
@interface SLQViewController () <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *_shops;
NSMutableArray *_deleteShops;
}
@end @implementation SLQViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 读取*.plist文件
// 1.获取全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
// 2.读取数据到数组
NSArray *array = [NSArray arrayWithContentsOfFile:path];
// 初始化数组
_shops = [NSMutableArray array];
_deleteShops = [NSMutableArray array];
//NSLog(@"%d",array.count);
// 添加数据到界面
for (NSDictionary *arr in array)
{
// 1.创建shop
Shop *s = [Shop shopWithDict:arr];
// 2.添加到数组
[_shops addObject:s];
}
//_buttonDelete.enabled = YES; } // 删除选中行
-(void)remove
{
// 1、删除行数据
[_shops removeObjectsInArray:_deleteShops];
// 2、删除_deleteShops数组
[_deleteShops removeAllObjects];
// 3、更新表格
[self.tableView reloadData];
} // 反选
- (void)unSelected
{
// 1、记录shops数组的长度和_deleteShops的长度
NSInteger shopsCount = _shops.count;
NSInteger deleteCount = _deleteShops.count; // 2、将数据全部添加到临时数组中,有先后顺序:shops在前,deleteshop在后
NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops];
[tmp addObjectsFromArray:_deleteShops]; // 3、添加数据到_deleteShops数组,取出前一部分
for (NSInteger i = ; i < shopsCount ;i ++)
{
Shop *s = [tmp objectAtIndex:i];
// 添加数据到_deleteShops数组
[_deleteShops addObject:s]; }
// 4、将取消选中的按钮从_deleteShops数组中移除数组范围(shopsCount,)后一部分,
for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++)
{
Shop *s = [tmp objectAtIndex:i];
[_deleteShops removeObject:s]; } // 5、更新表格
[_tableView reloadData];
}
// 全选\全不选按钮
- (void)selectAll
{
// 1、如果一样就清空deleteShop数组
if(_deleteShops.count == _shops.count)
{
[_deleteShops removeAllObjects];
}
// 2、否则就将shops数组中数据添加到deleteshops数组中
else
{
// 先清空deleteshop数组
[_deleteShops removeAllObjects];
// 再添加数据
for (NSInteger i = ; i < _shops.count ;i ++)
{
Shop *s = [_shops objectAtIndex:i];
// 添加数据到_deleteShops数组
[_deleteShops addObject:s]; }
}
// 3、更新表格
[_tableView reloadData];
}
// 设置行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 因为每次选中这两个值都会同时改变,所以放在这里会更好,可以省去很多代码
// 更新行时判断选中cell个数显示方式,每次改变都会调用
_textlable.text = (_deleteShops.count == ) ? @"淘宝" : [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
// 删除按钮状态
_buttonDelete.enabled = (_deleteShops.count == ) ? NO : YES;
// 反选按钮状态
_unSelectBtn.enabled = (_shops.count == ) ? NO : YES;
// 全选按钮状态
_selectAllBtn.enabled = (_shops.count == ) ? NO : YES;
return _shops.count; }
// 设置行内容
// 每当有一个cell进入视野范围内就会调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"C1";
// 从缓存池中选择可循环利用的cell,指定标识c1,这样就会找到结构一样的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果缓存池中没有
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 设定标识C1
}
// UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];
// 更新数据到界面
Shop *s = _shops[indexPath.row];
cell.textLabel.text = s.name;
cell.imageView.image = [UIImage imageNamed:s.icon];;
cell.detailTextLabel.text = s.desc;
// 显示最右侧的按钮
if ([_deleteShops containsObject:s]) // 判断是否已经选中的cell,是得话设置图标
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else // 否则就什么都不显示
{
cell.accessoryType = UITableViewCellAccessoryNone;
} // NSLog(@"%p,第%ld行数据",cell,indexPath.row); return cell;
}
// 设置每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//
//NSLog(@"height is 70");
return ;
}
// 选中某行执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"selected");
//选中后颜色变深
// 在最右侧显示一个对号图标
// 1、获得选中行
Shop *s = _shops[indexPath.row];
// 2、修改选中行的数据,将选中的cell添加到待删除数组中
if ([_deleteShops containsObject:s]) // 如果已经存在,再次点击就取消选中按钮
{
[_deleteShops removeObject:s];
}
else // 否则就添加待删除数组
{
[_deleteShops addObject:s];
}
// 3、更新数据
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// 4、显示选中条数
if(_deleteShops.count == )
{
_textlable.text = @"淘宝";
_buttonDelete.enabled = NO;
}
else
{
_textlable.text = [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
_buttonDelete.enabled = YES;
} }
// 取消选中某行执行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Deselected");
} @end
源代码:http://pan.baidu.com/s/1mgIIUEk
IOS开发学习笔记029-反选、全选、删除按钮的实现的更多相关文章
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- IOS开发学习笔记042-UITableView总结2
一.自定义非等高的cell 如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- IOS开发学习笔记028-UITableView单组数据显示代码优化
1.如果表格中又几百条数据的话,系统会自动加载显示在界面上得数据,逐一加载 添加100个数据到UITableView中 ; i < ; i ++) { NSString *icon = [NSS ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- IOS开发学习笔记016-Foundation框架
Foundation 框架的学习 一.Foundation 常用结构体 1.NSRange(location,length) typedef struct _NSRange { NSUIntege ...
- IOS开发学习笔记010-面向对象的三大特性
面向对象的三大特性 1.封装 2.继承 3.多态 一.封装 将类内部的属性保护起来,在外部不能直接访问,那么如果需要访问怎么办呢? OC提供了set方法来对成员变量进行访问 set方法 1.作用:提供 ...
随机推荐
- C# 字符串转组件名、变量名
字符串转组件名 (Controls["button1"] as Button).Text = "Hello";//单独组件 (Controls[].Contro ...
- pat甲级1016
1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the following ...
- linux 命令——52 ifconfig(转)
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- 漫谈 Clustering (1): k-means
好久没有写 blog 了,一来是 blog 下线一段时间,而租 DreamHost 的事情又一直没弄好:二来是没有太多时间,天天都跑去实验室.现在主要折腾 Machine Learning 相关的东西 ...
- PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)
http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...
- Bootstrap历练实例:响应式标签页
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- java Html&JavaScript面试题:判断第二个日期比第一个日期大
如何用脚本判断用户输入的的字符串是下面的时间格式2004-11-21 必须要保证用户的输入是此格式,并且是时间,比如说月份不大于12等等,另外我需要用户输入两个,并且后一个要比前一个晚,只允许用JAV ...
- JDK和CGLIB动态代理原理区别
JDK和CGLIB动态代理原理区别 https://blog.csdn.net/yhl_jxy/article/details/80635012 2018年06月09日 18:34:17 阅读数:65 ...
- shell脚本:变量,文件判断,逻辑运算等纪要
shell脚本中的变量定义,引用各有不同的方式,除此之外,很常用的有文件属性判断,逻辑运算,数值运算等,下面记录一下它们的属性作用 变量 shell变量的定义分为两种:一种是直接赋值定义,另一种是嵌套 ...
- 项目10.2-企业级自动化运维工具---puppet详解
1.认识puppet 1.1 引入 puppet是什么,咱们先不用专业的名词解释它,咱们先描述一些工作场景,看明白这些工作场景,自然会知道puppet是什么. (1)场景一: 管理员想要在100台服务 ...