iOS 全选 反选 和删除

还是在上一个程序的基础上进行修改

1、反选按钮

2、全选按钮

3、删除按钮

4、其他代码优化

 

1、反选按钮

反选的过程就是将_deleteShops数组中得数据清空,然后将Shops中数组添加到_deleteShops数组

 添加一个 UIBarButtonItem 按钮,绑定响应事件.

代码如下

复制代码
 1 // 反选
 2 - (void)unSelected
 3 {
 4     // 1、记录shops数组的长度和_deleteShops的长度
 5     NSInteger shopsCount = _shops.count;
 6     NSInteger deleteCount = _deleteShops.count;
 7 
 8     // 2、将数据全部添加到临时数组中,有先后顺序:shops在前,deleteshop在后
 9     NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops];
10     [tmp addObjectsFromArray:_deleteShops];
11     
12     // 3、添加数据到_deleteShops数组,取出前一部分
13     for (NSInteger i = 0 ; i < shopsCount ;i ++)
14     {
15         Shop *s = [tmp objectAtIndex:i];
16         // 添加数据到_deleteShops数组
17         [_deleteShops addObject:s];
18         
19     }
20     // 4、将取消选中的按钮从_deleteShops数组中移除数组范围(shopsCount,)后一部分,
21     for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++)
22     {
23         Shop *s = [tmp objectAtIndex:i];
24         [_deleteShops removeObject:s];
25        
26     }
27 
28     // 5、更新表格
29     [_tableView reloadData];
30 }
复制代码

 

2、全选\全不选按钮

全选\全不选按钮的实现主要在_deleteShops数组的数据进行增减

复制代码
 1 // 全选\全不选按钮
 2 - (void)selectAll
 3 {
 4     // 1、如果一样就清空deleteShop数组
 5     if(_deleteShops.count == _shops.count)
 6     {
 7         [_deleteShops removeAllObjects];
 8     }
 9     // 2、否则就将shops数组中数据添加到deleteshops数组中
10     else
11     {
12         // 先清空deleteshop数组
13         [_deleteShops removeAllObjects];
14         // 再添加数据
15         for (NSInteger i = 0 ; i < _shops.count ;i ++)
16         {
17             Shop *s = [_shops objectAtIndex:i];
18             // 添加数据到_deleteShops数组
19             [_deleteShops addObject:s];
20             
21         }
22     }
23     // 3、更新表格
24     [_tableView reloadData];
25 }
复制代码

 

3、删除按钮

 _deleteShops数组中保存的就是要删除的数据,直接从_shops数组中对数据进行删除就行

复制代码
 1 // 删除选中行
 2 -(void)remove
 3 {
 4     // 1、删除行数据
 5     [_shops removeObjectsInArray:_deleteShops];
 6     // 2、删除_deleteShops数组
 7     [_deleteShops removeAllObjects];
 8     // 3、更新表格
 9     [self.tableView reloadData];
10 }
复制代码

  

 4、其他代码实现

 在上述按钮按下的过程中会有几个控件的状态改变,删除按钮状态、选中行数量(lable标签)的状态、全选按钮状态、反选按钮状态。每次都会产生数据的更改,所以每次都需要对数据界面进行刷新。

所以把这些状态的改变放到方法numberOfRowsInSection中

复制代码
 1 // 设置行
 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 3 {
 4     // 因为每次选中这两个值都会同时改变,所以放在这里会更好,可以省去很多代码
 5     // 更新行时判断选中cell个数显示方式,每次改变都会调用
 6     _textlable.text = (_deleteShops.count == 0) ? @"淘宝" : [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
 7     // 删除按钮状态
 8     _buttonDelete.enabled = (_deleteShops.count == 0) ? NO : YES;
 9     // 反选按钮状态
10     _unSelectBtn.enabled = (_shops.count == 0) ? NO : YES;
11     // 全选按钮状态
12     _selectAllBtn.enabled = (_shops.count == 0) ? NO : YES;
13    return _shops.count;
14 
15 }
复制代码

 

 效果如下:

主要代码

复制代码
 1 //
 2 //  SLQViewController.h
 3 //  UITableView-淘宝
 4 //
 5 //  Created by Christian on 15/5/18.
 6 //  Copyright (c) 2015年 slq. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface SLQViewController : UIViewController
12 @property (weak, nonatomic) IBOutlet UILabel *textlable; // lable标签
13 @property (weak, nonatomic) IBOutlet UIBarButtonItem *buttonDelete; // 删除按钮
14 - (IBAction)remove; // 删除事件
15 - (IBAction)unSelected; // 反选事件
16 - (IBAction)selectAll; // 全选
17 @property (weak, nonatomic) IBOutlet UITableView *tableView; // tableView
18 @property (weak, nonatomic) IBOutlet UIBarButtonItem *unSelectBtn; // 反选按钮
19 @property (weak, nonatomic) IBOutlet UIBarButtonItem *selectAllBtn; // 全选按钮
20 
21 @end
复制代码

 

 

 

复制代码
  1 //
  2 //  SLQViewController.m
  3 //  UITableView-淘宝
  4 //
  5 //  Created by Christian on 15/5/18.
  6 //  Copyright (c) 2015年 slq. All rights reserved.
  7 //
  8 
  9 #import "SLQViewController.h"
 10 #import "Shop.h"
 11 @interface SLQViewController () <UITableViewDataSource, UITableViewDelegate>
 12 
 13 
 14 {
 15     NSMutableArray *_shops;
 16     NSMutableArray *_deleteShops;
 17 }
 18 @end
 19 
 20 @implementation SLQViewController
 21 
 22 - (void)viewDidLoad
 23 {
 24     [super viewDidLoad];
 25     // Do any additional setup after loading the view, typically from a nib.
 26     
 27     // 读取*.plist文件
 28     // 1.获取全路径
 29     NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
 30     // 2.读取数据到数组
 31     NSArray *array = [NSArray arrayWithContentsOfFile:path];
 32     // 初始化数组
 33     _shops  = [NSMutableArray array];
 34     _deleteShops = [NSMutableArray array];
 35     //NSLog(@"%d",array.count);
 36     // 添加数据到界面
 37     for (NSDictionary *arr in array)
 38     {
 39         // 1.创建shop
 40         Shop *s = [Shop shopWithDict:arr];
 41         // 2.添加到数组
 42         [_shops addObject:s];
 43     }
 44    //_buttonDelete.enabled = YES;
 45     
 46 }
 47 
 48 // 删除选中行
 49 -(void)remove
 50 {
 51     // 1、删除行数据
 52     [_shops removeObjectsInArray:_deleteShops];
 53     // 2、删除_deleteShops数组
 54     [_deleteShops removeAllObjects];
 55     // 3、更新表格
 56     [self.tableView reloadData];
 57 }
 58 
 59 // 反选
 60 - (void)unSelected
 61 {
 62     // 1、记录shops数组的长度和_deleteShops的长度
 63     NSInteger shopsCount = _shops.count;
 64     NSInteger deleteCount = _deleteShops.count;
 65 
 66     // 2、将数据全部添加到临时数组中,有先后顺序:shops在前,deleteshop在后
 67     NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops];
 68     [tmp addObjectsFromArray:_deleteShops];
 69     
 70     // 3、添加数据到_deleteShops数组,取出前一部分
 71     for (NSInteger i = 0 ; i < shopsCount ;i ++)
 72     {
 73         Shop *s = [tmp objectAtIndex:i];
 74         // 添加数据到_deleteShops数组
 75         [_deleteShops addObject:s];
 76         
 77     }
 78     // 4、将取消选中的按钮从_deleteShops数组中移除数组范围(shopsCount,)后一部分,
 79     for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++)
 80     {
 81         Shop *s = [tmp objectAtIndex:i];
 82         [_deleteShops removeObject:s];
 83        
 84     }
 85 
 86     // 5、更新表格
 87     [_tableView reloadData];
 88 }
 89 // 全选\全不选按钮
 90 - (void)selectAll
 91 {
 92     // 1、如果一样就清空deleteShop数组
 93     if(_deleteShops.count == _shops.count)
 94     {
 95         [_deleteShops removeAllObjects];
 96     }
 97     // 2、否则就将shops数组中数据添加到deleteshops数组中
 98     else
 99     {
100         // 先清空deleteshop数组
101         [_deleteShops removeAllObjects];
102         // 再添加数据
103         for (NSInteger i = 0 ; i < _shops.count ;i ++)
104         {
105             Shop *s = [_shops objectAtIndex:i];
106             // 添加数据到_deleteShops数组
107             [_deleteShops addObject:s];
108             
109         }
110     }
111     // 3、更新表格
112     [_tableView reloadData];
113 }
114 // 设置行
115 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
116 {
117     // 因为每次选中这两个值都会同时改变,所以放在这里会更好,可以省去很多代码
118     // 更新行时判断选中cell个数显示方式,每次改变都会调用
119     _textlable.text = (_deleteShops.count == 0) ? @"淘宝" : [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
120     // 删除按钮状态
121     _buttonDelete.enabled = (_deleteShops.count == 0) ? NO : YES;
122     // 反选按钮状态
123     _unSelectBtn.enabled = (_shops.count == 0) ? NO : YES;
124     // 全选按钮状态
125     _selectAllBtn.enabled = (_shops.count == 0) ? NO : YES;
126    return _shops.count;
127 
128 }
129 // 设置行内容
130 // 每当有一个cell进入视野范围内就会调用
131 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
132 {
133     static NSString *ID = @"C1";
134     // 从缓存池中选择可循环利用的cell,指定标识c1,这样就会找到结构一样的cell
135     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
136     // 如果缓存池中没有
137     if (cell == nil)
138     {
139         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 设定标识C1
140     }
141     // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];
142     // 更新数据到界面
143     Shop *s = _shops[indexPath.row];
144     cell.textLabel.text = s.name;
145     cell.imageView.image = [UIImage imageNamed:s.icon];;
146     cell.detailTextLabel.text = s.desc;
147     // 显示最右侧的按钮
148     if ([_deleteShops containsObject:s]) // 判断是否已经选中的cell,是得话设置图标
149     {
150         cell.accessoryType = UITableViewCellAccessoryCheckmark;
151     }
152     else    // 否则就什么都不显示
153     {
154         cell.accessoryType = UITableViewCellAccessoryNone;
155     }
156     
157    // NSLog(@"%p,第%ld行数据",cell,indexPath.row);
158     
159     return cell;
160 }
161 // 设置每一行的高度
162 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
163 {
164     //
165     //NSLog(@"height is 70");
166     return 100;
167 }
168 // 选中某行执行
169 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
170 {
171     //NSLog(@"selected");
172     //选中后颜色变深
173     // 在最右侧显示一个对号图标
174     // 1、获得选中行
175     Shop *s = _shops[indexPath.row];
176     // 2、修改选中行的数据,将选中的cell添加到待删除数组中
177     if ([_deleteShops containsObject:s]) // 如果已经存在,再次点击就取消选中按钮
178     {
179         [_deleteShops removeObject:s];
180     }
181     else    // 否则就添加待删除数组
182     {
183         [_deleteShops addObject:s];
184     }
185     // 3、更新数据
186     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
187     // 4、显示选中条数
188     if(_deleteShops.count == 0)
189     {
190         _textlable.text = @"淘宝";
191         _buttonDelete.enabled = NO;
192     }
193     else
194     {
195         _textlable.text = [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
196         _buttonDelete.enabled = YES;
197     }
198     
199 }
200 // 取消选中某行执行
201 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
202 {
203     NSLog(@"Deselected");
204 }
205 
206 
207 @end
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值