当前位置: 首页 > news >正文

UITableView多选全选

自定义cell和取到相应的cell就行了

TableViewCell.h

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell {
    BOOL _checked;
    UIImageView *_checkedImage;
}

- (void)setChecked:(BOOL)checked;

@end

TableViewCell.m

#import "TableViewCell.h"

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _checkedImage = [[UIImageView alloc]init];
        _checkedImage.image = [UIImage imageNamed:@"Unselected"];
        [self.contentView addSubview:_checkedImage];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _checkedImage.frame = CGRectMake(10, 10, 29, 29);
}

- (void)setChecked:(BOOL)checked {
    if (checked) {
        _checkedImage.image = [UIImage imageNamed:@"Selected"];
    }else {
        _checkedImage.image = [UIImage imageNamed:@"Unselected"];
    }
  _checked = checked; }
- (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end

ViewController.m

#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *_tableView;
}

@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSMutableArray *checkedArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initDataSource];
    self.view.backgroundColor = [UIColor lightGrayColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"全选" forState:UIControlStateNormal];
    [button setTitle:@"取消" forState:UIControlStateSelected];
    button.frame = CGRectMake(10, 10, 100, 50);
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)initDataSource {
    _checkedArray = [NSMutableArray array];
    for (int i = 0; i < self.array.count; i ++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:@"NO" forKey:@"checked"];
        [_checkedArray addObject:dic];
    }
}

#pragma mark - 懒加载

- (NSMutableArray *)array {
    if (!_array) {
        _array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    }
    return _array;
}

#pragma mark - 事件监听

- (void)buttonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_tableView indexPathsForVisibleRows]];
    for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
        NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
        //取得对应的cell
        TableViewCell *cell = (TableViewCell*)[_tableView cellForRowAtIndexPath:indexPath];
        NSUInteger row = [indexPath row];
        NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];
        if (sender.selected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSUInteger row = indexPath.row;
    [self cellChecked:cell row:row isSelected:NO];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TableViewCell *cell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSUInteger row = indexPath.row;
    [self cellChecked:cell row:row isSelected:YES];
}
#pragma mark - others

/**
 *  点击,和加载cell的时候进行判断,从而改变cell的选中状态
 *
 *  @param cell       自定义的cell
 *  @param row        tableView的下标
 *  @param isSelected 是否是点击
 */

- (void)cellChecked:(TableViewCell *)cell row:(NSUInteger)row isSelected:(BOOL)isSelected{
    
    NSMutableDictionary *dic = [_checkedArray objectAtIndex:row];
    if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
        if (isSelected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }else {
        if (!isSelected) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
}

效果图:

这是以前写的老版本,新版本请看:

http://www.cnblogs.com/hxwj/p/4536499.html 

新版demo下载地址:http://pan.baidu.com/s/1o6DpN0u

修改了一个全选的bug:https://github.com/WuJiForFantasy/UITableViewChooseDelete-.git

 

转载于:https://www.cnblogs.com/hxwj/p/4532172.html

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 简述WebService与.NET Remoting的区别及适应场合 WCF
  • C#为工作Sql而产生的字符串分割小工具(很实用,你值得拥有)
  • mongodb安装-配置文件
  • 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二十六:VGA模块
  • 开源收藏
  • MDT 2013 从入门到精通之磁盘分区调整
  • High Quality Coding
  • 精心收集的必须熟悉的vim快捷键操作
  • Game publishing request was abnormally terminated (ID 27492).
  • iOS8推送消息的回复处理速度
  • Tiny模板编辑器
  • windows整体性能检测
  • 我的书签
  • 16 行为型模式-----备忘录模式
  • Nodejs部署再思考
  • 07.Android之多媒体问题
  • android 一些 utils
  • create-react-app项目添加less配置
  • es6--symbol
  • flutter的key在widget list的作用以及必要性
  • HTML中设置input等文本框为不可操作
  • java小心机(3)| 浅析finalize()
  • Making An Indicator With Pure CSS
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • 从0实现一个tiny react(三)生命周期
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 每天一个设计模式之命令模式
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 在weex里面使用chart图表
  • 走向全栈之MongoDB的使用
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • #include到底该写在哪
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • $.ajax()方法详解
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (SpringBoot)第二章:Spring创建和使用
  • (附源码)c#+winform实现远程开机(广域网可用)
  • (回溯) LeetCode 77. 组合
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (三)终结任务
  • (一)基于IDEA的JAVA基础1
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • [ NOI 2001 ] 食物链
  • [AIGC] 广度优先搜索(Breadth-First Search,BFS)详解
  • [ARM]ldr 和 adr 伪指令的区别
  • [DAU-FI Net开源 | Dual Attention UNet+特征融合+Sobel和Canny等算子解决语义分割痛点]
  • [Django 0-1] Core.Email 模块
  • [G-CS-MR.PS02] 機巧之形2: Ruler Circle
  • [GXYCTF2019]BabyUpload1 -- 题目分析与详解