iOS tableView多选,全选

实现了多个分组的卷展显示,单行选择及分组全选的功能

首先定义需要是用到的几个数组及tableview

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @property(nonatomic,strong)NSMutableArray * showSectionArray;  
  2. @property(nonatomic,strong)NSMutableArray * seleStudentArray;  
  3. @property(nonatomic,strong)NSMutableArray * seleClassArray;  
  4. @property(nonatomic,strong)UITableView * tableView;  


首先是tableview和顶部按钮的创建

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     self.showSectionArray = [NSMutableArray array];  
  4.     self.seleStudentArray = [NSMutableArray array];  
  5.     self.seleClassArray = [NSMutableArray array];  
  6.     UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(040, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStyleGrouped];  
  7.     [self.view addSubview:tableView];  
  8.     tableView.delegate = self;  
  9.     tableView.dataSource = self;  
  10.     self.tableView = tableView;  
  11.       
  12.     UIButton * subBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
  13.     [subBtn setTitle:@"提交" forState:UIControlStateNormal];  
  14.     subBtn.frame = CGRectMake(020, SCREEN_HEIGHT, 20);  
  15.     subBtn.backgroundColor = [UIColor redColor];  
  16.     [subBtn addTarget:self action:@selector(subMit) forControlEvents:UIControlEventTouchUpInside];  
  17.     [self.view addSubview:subBtn];  
  18. }  
这里随手设置了两个fram不要在意


然后是tableview的各个代理方法的实现

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  2.     if ([self hasShowSection:section]) {  
  3.         return 0;  
  4.     }else{  
  5.           
  6.         return 12;  
  7.     }  
  8. }  
  9.   
  10. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  11.   
  12.     return 7;  
  13. }  
  14.   
  15. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  16.     ZSeleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"idcell"];  
  17.     if (!cell) {  
  18.         cell = [[ZSeleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"idcell"];  
  19.     }  
  20.     cell.nameLable.text = [NSString stringWithFormat:@"第%ld个学生",(long)indexPath.row];  
  21.   
  22.     if ([self hasShowSeleRow:indexPath] ) {  
  23.         cell.leftBtn.selected = YES;  
  24.     }else{  
  25.         cell.leftBtn.selected = NO;  
  26.     }  
  27.     [cell.leftBtn addTarget:self action:@selector(addSele:) forControlEvents:UIControlEventTouchUpInside];  
  28.     cell.leftBtn.tag = 1100000 * indexPath.section + indexPath.row;  
  29.     return cell;  
  30. }  
  31.   
  32. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  33.     [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  34.     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];  
  35.     button.tag = 1100000 * indexPath.section + indexPath.row;  
  36.     [self addSele:button];  
  37. }  
  38.   
  39. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{  
  40.     return 0.1;  
  41. }  
  42.   
  43. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
  44.     return 30;  
  45. }  
  46.   
  47. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  48.     return 38;  
  49. }  
  50.   
  51. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{  
  52.     ZseleHeadView * headView = [[ZseleHeadView alloc] initWithFrame:CGRectMake(00, SCREEN_WIDTH, 30) WithSection:section];  
  53.       
  54.     headView.showBtn.selected = [self hasShowSection:section];  
  55.     [headView.showBtn addTarget:self action:@selector(addShowSec:) forControlEvents:UIControlEventTouchUpInside];  
  56.       
  57.     headView.seleClassBtn.selected = [self hasShowSeleClass:section];  
  58.     [headView.seleClassBtn addTarget:self action:@selector(addClassSele:) forControlEvents:UIControlEventTouchUpInside];  
  59.       
  60.     if ([self hasShowSection:section]) {  
  61.         headView.seleImageView.transform = CGAffineTransformMakeRotation(M_PI * (-0.5));  
  62.     }else{  
  63.         headView.seleImageView.transform = CGAffineTransformMakeRotation(M_PI * 0);  
  64.     }  
  65.       
  66.     reurn headView;  
  67. }  

其中cell和headView都是自定义的类,代码如下

ZSeleCell.h

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ZSeleCell : UITableViewCell  
  4.   
  5. @property(nonatomic,strong)UIButton * leftBtn;  
  6. @property(nonatomic,strong)UILabel * nameLable;  
  7.   
  8. @end  

ZSeleCell.m


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="objc">#import "ZSeleCell.h"  
  2.   
  3. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width  
  4. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height  
  5. @implementation ZSeleCell  
  6.   
  7. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  
  8.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  9.     if (self) {  
  10.         self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
  11.         [self.leftBtn setImage:[UIImage imageNamed:@"ico_aa"] forState:UIControlStateNormal];  
  12.         [self.leftBtn setImage:[UIImage imageNamed:@"ico_aaS"] forState:UIControlStateSelected];  
  13.         self.leftBtn.frame = CGRectMake(15101818);  
  14.         [self addSubview:self.leftBtn];  
  15.           
  16.         self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(4010, SCREEN_WIDTH - 5018)];  
  17.         [self addSubview:self.nameLable];  
  18.         self.nameLable.textAlignment = NSTextAlignmentLeft;  
  19.           
  20.           
  21.     }  
  22.     return self;  
  23. }  
  24.   
  25. @end  
 

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="objc">ZseleHeadView.h  

 
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ZseleHeadView : UIView  
  4.   
  5. @property(nonatomic,strong)UIButton * showBtn;  
  6. @property(nonatomic,strong)UIButton * seleClassBtn;  
  7. @property(nonatomic,strong)UIImageView * seleImageView;  
  8.   
  9. -(instancetype)initWithFrame:(CGRect)frame WithSection:(NSInteger)section;  
  10. @end  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ZseleHeadView.m  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "ZseleHeadView.h"  
  2.   
  3. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width  
  4. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height  
  5. @implementation ZseleHeadView  
  6.   
  7. -(instancetype)initWithFrame:(CGRect)frame WithSection:(NSInteger)section{  
  8.     self = [super initWithFrame:frame];  
  9.     if (self) {  
  10.         self.backgroundColor = [UIColor blueColor];  
  11.         UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];  
  12.         button.frame = self.frame;  
  13.         button.tag = 1000 + section;  
  14.         [button setTitle:[NSString stringWithFormat:@"      第%ld个班级",(long)section] forState:UIControlStateNormal];  
  15.         button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;  
  16.         [self addSubview:button];  
  17.         self.showBtn = button;  
  18.           
  19.         UIButton * seleClassButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  20.         [seleClassButton setImage:[UIImage imageNamed:@"ico_aa"] forState:UIControlStateNormal];  
  21.         [seleClassButton setImage:[UIImage imageNamed:@"ico_aaS"] forState:UIControlStateSelected];  
  22.         seleClassButton.tag = 1000000 + section;  
  23.         seleClassButton.frame = CGRectMake(SCREEN_WIDTH - 3061818);  
  24.         [self addSubview:seleClassButton];  
  25.         self.seleClassBtn = seleClassButton;  
  26.           
  27.         UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(1010128)];  
  28.         [imageView setImage:[UIImage imageNamed:@"icon_xl"]];  
  29.   
  30.         [self addSubview:imageView];  
  31.         self.seleImageView = imageView;  
  32.           
  33.     }  
  34.     return self;  
  35. }  
  36.   
  37. @end  

最后是几个添加方法和判断方法的实现

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma mark 该组是否展示  
  2. -(BOOL)hasShowSection:(NSInteger)section{  
  3.     for (int i =0; i < self.showSectionArray.count; i++) {  
  4.         NSInteger show = [self.showSectionArray[i] integerValue];  
  5.         if (show == section) {  
  6.             return YES;  
  7.         }  
  8.     }  
  9.     return NO;  
  10. }  
  11.   
  12. #pragma mark 该学生是否选中  
  13. -(BOOL)hasShowSeleRow:(NSIndexPath *)index{  
  14.     for (int i =0; i < self.seleStudentArray.count; i++) {  
  15.         NSIndexPath * show = self.seleStudentArray[i] ;  
  16.         if (show == index) {  
  17.             return YES;  
  18.         }  
  19.     }  
  20.     return NO;  
  21. }  
  22.   
  23. #pragma mark 该组是否全部选中  
  24. -(BOOL)hasShowSeleClass:(NSInteger )section{  
  25.     for (int i =0; i < self.seleClassArray.count; i++) {  
  26.         NSInteger show = [self.seleClassArray[i] integerValue];  
  27.         if (show == section) {  
  28.             return YES;  
  29.         }  
  30.     }  
  31.     return NO;  
  32. }  
  33.   
  34. #pragma mark 该组选中人数  
  35. -(NSInteger)numberOfSeleForClass:(NSInteger)section{  
  36.     NSInteger stuNum = 0;  
  37.     for (NSIndexPath * path in self.seleStudentArray) {  
  38.         if (path.section == section) {  
  39.             stuNum ++;  
  40.         }  
  41.     }  
  42.     return stuNum;  
  43. }  
  44.   
  45. #pragma mark 添加展示的组  
  46. -(void)addShowSec:(UIButton *)button{  
  47.     button.selected = !button.selected;  
  48.     NSNumber * number = [NSNumber numberWithInteger:button.tag - 1000];  
  49.     if (button.selected == YES) {  
  50.           
  51.         [self.showSectionArray addObject:number];  
  52.     }else{  
  53.         [self.showSectionArray removeObject:number];  
  54.     }  
  55.       
  56.     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[number integerValue]] withRowAnimation:UITableViewRowAnimationTop];  
  57. }  
  58.   
  59. #pragma mark 添加学生  
  60. -(void)addSele:(UIButton *)button{  
  61.     NSInteger section = button.tag / 100000;  
  62.     NSInteger row = button.tag % 100000;  
  63.     NSIndexPath * path = [NSIndexPath indexPathForItem:row inSection:section];  
  64.       
  65.     ZSeleCell * cell = (ZSeleCell *)[self tableView:self.tableView cellForRowAtIndexPath:path];  
  66.     cell.leftBtn.selected = !cell.leftBtn.selected;  
  67.       
  68.     if (cell.leftBtn.selected) {  
  69.           
  70.         [self.seleStudentArray addObject:path];  
  71.         if ([self numberOfSeleForClass:section] == 12) {  
  72.             [self.seleClassArray addObject:[NSNumber numberWithInteger:section]];  
  73.         }  
  74.     }else{  
  75.         [self.seleStudentArray removeObject:path];  
  76.         [self.seleClassArray removeObject:[NSNumber numberWithInteger:section]];  
  77.     }  
  78.     NSIndexSet * index = [NSIndexSet indexSetWithIndex:section];  
  79.     [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];  
  80. //    [self.tableView reloadData];  
  81. }  
  82.   
  83. #pragma mark 添加选中班级  
  84. -(void)addClassSele:(UIButton *)button{  
  85.     button.selected = !button.selected;  
  86.     NSNumber * number = [NSNumber numberWithInteger:button.tag - 1000000];  
  87.     if (button.selected == YES) {  
  88.         [self.seleClassArray addObject:number];  
  89.         for (int i = 0; i < 12; i ++) {  
  90.             NSIndexPath * path = [NSIndexPath indexPathForItem:i inSection:[number integerValue]];  
  91.             if ([self hasShowSeleRow:path]) {  
  92.                 [self.seleStudentArray removeObject:path];  
  93.             }  
  94.             [self.seleStudentArray addObject:path];  
  95.         }  
  96.     }else{  
  97.         [self.seleClassArray removeObject:number];  
  98.         for (int i = 0; i < 12; i ++) {  
  99.             NSIndexPath * path = [NSIndexPath indexPathForItem:i inSection:[number integerValue]];  
  100.             [self.seleStudentArray removeObject:path];  
  101.         }  
  102.     }  
  103.       
  104.     [self.tableView reloadData];  
  105. }  

最后的最后,顶部按钮的点击事件,点击可以在控制台打印出当前选择了的学生

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(void)subMit{  
  2.     for (NSIndexPath * path in self.seleStudentArray) {  
  3.         NSLog(@"第%ld班级   第%ld个学生被选中",(long)path.section,(long)path.row);  
  4.     }  
  5. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值