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];
        }
    }
}

效果图:

时间: 2024-08-09 23:54:02

UITableView多选全选的相关文章

Dynamic CRM 2013学习笔记(二十六)Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色

上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数可以多选或全选:动态地显示列,列不是固定的:根据某一字段的值来动态地显示整行字体的颜色. 下面介绍详细的方法: 一.动态参数.参数多选全选 动态参数: 首先定义一个Dataset: SELECT DISTINCT new_countryId, new_codename AS name FROM ne

js多选全选

1 <style type="text/css"> 2 table { 3 width: 800px; 4 text-align: left; 5 border-collapse: collapse; 6 } 7 8 td, th { 9 padding: 10px; 10 border: 1px solid black; 11 } 12 </style> 13 <script type="text/javascript"> 14

angular js实现多选 全选

前端代码 1 <ul class="list checkstyle"> 2 <li class="item item-checkbox"> 3 <label class="checkbox checkbox-balanced"> 4 <input type="checkbox" ng-model="master" ng-click="all(master,

listview实现多选 全选 反选

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

vue-element 动态单选多选全选

实现效果如图 数据格式如下: pps: [{"code":"6","createTime":"2018-09-07 00:00:00","des":"可选择居住情况","id":"6","key":"liveType","multi":false,"name":&quo

layui 复选框checkbox 实现全选全选

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="layui/css/layui.css" media="all"> </head> &l

全选与反选

我们的页面上有一个歌曲列表,列出多行歌曲名称,并匹配复选框供用户选择,并且在列表下方有一排操作按钮. <ul id="list"> <li><label><input type="checkbox" value="1"> 1.时间都去哪儿了</label></li> <li><label><input type="checkbox&qu

2016/3/30 租房子 ①建立租房子的增、删、改php页面 ②多条件查询 ③全选时 各部分全选中 任意checkbox不选中 全选checkbox不选中

字符串的另一种写法:<<<AAAA; 后两个AA回车要求定格  不然报错 例子: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <?php 9 $str = &

用jquery实现复选框全选全不选问题(完整版),在网络上怎么也找不到完整的解决方案,重要搞全了

首先准备jsp页面控件: 请选择您的爱好:<br> <input type="checkbox" id="all" name="selectAll"value="全选"/>全选<br> <input type="checkbox" name="hobby"value="足球"/>足球 <input type=&qu