本文主要是介绍IOSUITableView 反选 全选 删除按钮的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@interface Shop : NSObject
@property (nonatomic,copy) NSString *icon; // 图片
@property (nonatomic,copy) NSString *name; // 名称
@property (nonatomic,copy) NSString *desc; // 描述
- (id)initWithDict:(NSDictionary *)dict;
+ (id)shopWithDict:(NSDictionary *)dict;
@implementation Shop
- (id)initWithDict:(NSDictionary *)dict
{
Shop *shop = [[Shop alloc] init];
shop.icon = dict[@"icon"];
shop.name = dict[@"name"];
shop.desc = dict[@"desc"];
return shop;
}
+ (id)shopWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@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
#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 = 0 ; 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 = 0 ; 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 == 0) ? @"淘宝" : [NSString stringWithFormat:@"淘宝(%d)",_deleteShops.count];
// 删除按钮状态
_buttonDelete.enabled = (_deleteShops.count == 0) ? NO : YES;
// 反选按钮状态
_unSelectBtn.enabled = (_shops.count == 0) ? NO : YES;
// 全选按钮状态
_selectAllBtn.enabled = (_shops.count == 0) ? 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 100;
}
// 选中某行执行
- (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 == 0)
{
_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://www.it165.net/pro/html/201505/41553.html
这篇关于IOSUITableView 反选 全选 删除按钮的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!