IOSUITableView 反选 全选 删除按钮的实现

2024-02-28 15:18

本文主要是介绍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 反选 全选 删除按钮的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/755865

相关文章

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系