实现了多个分组的卷展显示,单行选择及分组全选的功能
首先定义需要是用到的几个数组及tableview
- @property(nonatomic,strong)NSMutableArray * showSectionArray;
- @property(nonatomic,strong)NSMutableArray * seleStudentArray;
- @property(nonatomic,strong)NSMutableArray * seleClassArray;
- @property(nonatomic,strong)UITableView * tableView;
首先是tableview和顶部按钮的创建
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.showSectionArray = [NSMutableArray array];
- self.seleStudentArray = [NSMutableArray array];
- self.seleClassArray = [NSMutableArray array];
- UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStyleGrouped];
- [self.view addSubview:tableView];
- tableView.delegate = self;
- tableView.dataSource = self;
- self.tableView = tableView;
- UIButton * subBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [subBtn setTitle:@"提交" forState:UIControlStateNormal];
- subBtn.frame = CGRectMake(0, 20, SCREEN_HEIGHT, 20);
- subBtn.backgroundColor = [UIColor redColor];
- [subBtn addTarget:self action:@selector(subMit) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:subBtn];
- }
然后是tableview的各个代理方法的实现
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- if ([self hasShowSection:section]) {
- return 0;
- }else{
- return 12;
- }
- }
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 7;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- ZSeleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"idcell"];
- if (!cell) {
- cell = [[ZSeleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"idcell"];
- }
- cell.nameLable.text = [NSString stringWithFormat:@"第%ld个学生",(long)indexPath.row];
- if ([self hasShowSeleRow:indexPath] ) {
- cell.leftBtn.selected = YES;
- }else{
- cell.leftBtn.selected = NO;
- }
- [cell.leftBtn addTarget:self action:@selector(addSele:) forControlEvents:UIControlEventTouchUpInside];
- cell.leftBtn.tag = 1100000 * indexPath.section + indexPath.row;
- return cell;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.tag = 1100000 * indexPath.section + indexPath.row;
- [self addSele:button];
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- return 0.1;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- return 30;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 38;
- }
- -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
- ZseleHeadView * headView = [[ZseleHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30) WithSection:section];
- headView.showBtn.selected = [self hasShowSection:section];
- [headView.showBtn addTarget:self action:@selector(addShowSec:) forControlEvents:UIControlEventTouchUpInside];
- headView.seleClassBtn.selected = [self hasShowSeleClass:section];
- [headView.seleClassBtn addTarget:self action:@selector(addClassSele:) forControlEvents:UIControlEventTouchUpInside];
- if ([self hasShowSection:section]) {
- headView.seleImageView.transform = CGAffineTransformMakeRotation(M_PI * (-0.5));
- }else{
- headView.seleImageView.transform = CGAffineTransformMakeRotation(M_PI * 0);
- }
- reurn headView;
- }
其中cell和headView都是自定义的类,代码如下
ZSeleCell.h
- #import <UIKit/UIKit.h>
- @interface ZSeleCell : UITableViewCell
- @property(nonatomic,strong)UIButton * leftBtn;
- @property(nonatomic,strong)UILabel * nameLable;
- @end
ZSeleCell.m
- <pre name="code" class="objc">#import "ZSeleCell.h"
- #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
- #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
- @implementation ZSeleCell
- -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [self.leftBtn setImage:[UIImage imageNamed:@"ico_aa"] forState:UIControlStateNormal];
- [self.leftBtn setImage:[UIImage imageNamed:@"ico_aaS"] forState:UIControlStateSelected];
- self.leftBtn.frame = CGRectMake(15, 10, 18, 18);
- [self addSubview:self.leftBtn];
- self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, SCREEN_WIDTH - 50, 18)];
- [self addSubview:self.nameLable];
- self.nameLable.textAlignment = NSTextAlignmentLeft;
- }
- return self;
- }
- @end
- <pre name="code" class="objc">ZseleHeadView.h
- #import <UIKit/UIKit.h>
- @interface ZseleHeadView : UIView
- @property(nonatomic,strong)UIButton * showBtn;
- @property(nonatomic,strong)UIButton * seleClassBtn;
- @property(nonatomic,strong)UIImageView * seleImageView;
- -(instancetype)initWithFrame:(CGRect)frame WithSection:(NSInteger)section;
- @end
- ZseleHeadView.m
- #import "ZseleHeadView.h"
- #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
- #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
- @implementation ZseleHeadView
- -(instancetype)initWithFrame:(CGRect)frame WithSection:(NSInteger)section{
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor blueColor];
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = self.frame;
- button.tag = 1000 + section;
- [button setTitle:[NSString stringWithFormat:@" 第%ld个班级",(long)section] forState:UIControlStateNormal];
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- [self addSubview:button];
- self.showBtn = button;
- UIButton * seleClassButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [seleClassButton setImage:[UIImage imageNamed:@"ico_aa"] forState:UIControlStateNormal];
- [seleClassButton setImage:[UIImage imageNamed:@"ico_aaS"] forState:UIControlStateSelected];
- seleClassButton.tag = 1000000 + section;
- seleClassButton.frame = CGRectMake(SCREEN_WIDTH - 30, 6, 18, 18);
- [self addSubview:seleClassButton];
- self.seleClassBtn = seleClassButton;
- UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 12, 8)];
- [imageView setImage:[UIImage imageNamed:@"icon_xl"]];
- [self addSubview:imageView];
- self.seleImageView = imageView;
- }
- return self;
- }
- @end
最后是几个添加方法和判断方法的实现
- #pragma mark 该组是否展示
- -(BOOL)hasShowSection:(NSInteger)section{
- for (int i =0; i < self.showSectionArray.count; i++) {
- NSInteger show = [self.showSectionArray[i] integerValue];
- if (show == section) {
- return YES;
- }
- }
- return NO;
- }
- #pragma mark 该学生是否选中
- -(BOOL)hasShowSeleRow:(NSIndexPath *)index{
- for (int i =0; i < self.seleStudentArray.count; i++) {
- NSIndexPath * show = self.seleStudentArray[i] ;
- if (show == index) {
- return YES;
- }
- }
- return NO;
- }
- #pragma mark 该组是否全部选中
- -(BOOL)hasShowSeleClass:(NSInteger )section{
- for (int i =0; i < self.seleClassArray.count; i++) {
- NSInteger show = [self.seleClassArray[i] integerValue];
- if (show == section) {
- return YES;
- }
- }
- return NO;
- }
- #pragma mark 该组选中人数
- -(NSInteger)numberOfSeleForClass:(NSInteger)section{
- NSInteger stuNum = 0;
- for (NSIndexPath * path in self.seleStudentArray) {
- if (path.section == section) {
- stuNum ++;
- }
- }
- return stuNum;
- }
- #pragma mark 添加展示的组
- -(void)addShowSec:(UIButton *)button{
- button.selected = !button.selected;
- NSNumber * number = [NSNumber numberWithInteger:button.tag - 1000];
- if (button.selected == YES) {
- [self.showSectionArray addObject:number];
- }else{
- [self.showSectionArray removeObject:number];
- }
- [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[number integerValue]] withRowAnimation:UITableViewRowAnimationTop];
- }
- #pragma mark 添加学生
- -(void)addSele:(UIButton *)button{
- NSInteger section = button.tag / 100000;
- NSInteger row = button.tag % 100000;
- NSIndexPath * path = [NSIndexPath indexPathForItem:row inSection:section];
- ZSeleCell * cell = (ZSeleCell *)[self tableView:self.tableView cellForRowAtIndexPath:path];
- cell.leftBtn.selected = !cell.leftBtn.selected;
- if (cell.leftBtn.selected) {
- [self.seleStudentArray addObject:path];
- if ([self numberOfSeleForClass:section] == 12) {
- [self.seleClassArray addObject:[NSNumber numberWithInteger:section]];
- }
- }else{
- [self.seleStudentArray removeObject:path];
- [self.seleClassArray removeObject:[NSNumber numberWithInteger:section]];
- }
- NSIndexSet * index = [NSIndexSet indexSetWithIndex:section];
- [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
- // [self.tableView reloadData];
- }
- #pragma mark 添加选中班级
- -(void)addClassSele:(UIButton *)button{
- button.selected = !button.selected;
- NSNumber * number = [NSNumber numberWithInteger:button.tag - 1000000];
- if (button.selected == YES) {
- [self.seleClassArray addObject:number];
- for (int i = 0; i < 12; i ++) {
- NSIndexPath * path = [NSIndexPath indexPathForItem:i inSection:[number integerValue]];
- if ([self hasShowSeleRow:path]) {
- [self.seleStudentArray removeObject:path];
- }
- [self.seleStudentArray addObject:path];
- }
- }else{
- [self.seleClassArray removeObject:number];
- for (int i = 0; i < 12; i ++) {
- NSIndexPath * path = [NSIndexPath indexPathForItem:i inSection:[number integerValue]];
- [self.seleStudentArray removeObject:path];
- }
- }
- [self.tableView reloadData];
- }
最后的最后,顶部按钮的点击事件,点击可以在控制台打印出当前选择了的学生
- -(void)subMit{
- for (NSIndexPath * path in self.seleStudentArray) {
- NSLog(@"第%ld班级 第%ld个学生被选中",(long)path.section,(long)path.row);
- }
- }