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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值