#import "MGMineMenuVc.h"
#import "MGSubSelectVc.h"
#import "HeaderView.h"// 此为自定义的分区视图
#import "PlCustormCell.h" //自定义单元格
static NSString * const ReuseIdentifierHeader = @"header";
static NSString * const ReuseIdentifierCell = @"dcell";
@interface MGMineMenuVc ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, copy) MGBasicBlock basicBlock;
@property(nonatomic,strong)UISwipeGestureRecognizer * rightGesture;
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)UILabel * lab;
@property(nonatomic,assign)BOOL isPieceSelected;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSMutableArray *expendArray;//用于记录分区字段的数组
@property (nonatomic, strong) NSMutableArray *selectArray;//用于记录单元格具体字段的数组
@end
@implementation MGMineMenuVc
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"筛选";
self.isPieceSelected = NO;
UIBarButtonItem *cancelBarItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelBarItem;
UIBarButtonItem *SureBarItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelBarItem;
self.navigationItem.rightBarButtonItem = SureBarItem;
[self loadData];
[self initTableView];
self.rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightGestureRec:)];
[self.view addGestureRecognizer:self.rightGesture];
self.rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
}
-(void)rightGestureRec:(UISwipeGestureRecognizer *)sender
{
[self cancelAction];
}
-(void)loadData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"role" ofType:@"plist"];
NSMutableArray *wai = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (int i = 0; i < wai.count; i ++)
{
NSArray *list = [wai[i] objectForKey:@"list"];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
for (int j = 0; j < list.count; j ++) {
[array addObject:[list objectAtIndex:j]];
}
[self.dataDic setObject:array forKey:[wai[i] objectForKey:@"name"]];
}
self.dataArray = [self.dataDic allKeys];
}
-(void)initTableView
{
self.tableView.tableFooterView = [UIView new];
[self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, Screen_weight-kMGLeftSpace, Screen_height) style:UITableViewStyleGrouped];
[self.tableView registerClass:[HeaderView class] forHeaderFooterViewReuseIdentifier:ReuseIdentifierHeader];
[self.tableView registerClass:[PlCustormCell class] forCellReuseIdentifier:ReuseIdentifierCell];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (void)setCancleBarItemHandle:(MGBasicBlock)basicBlock{
self.basicBlock = basicBlock;
}
- (void)cancelAction{
if(self.basicBlock)self.basicBlock();
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *key = self.dataArray[section];
NSArray *array = self.dataDic[key];
if ([self.expendArray containsObject:key]) {
return array.count;
}else {
return 0.0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *key = self.dataArray[section];
HeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ReuseIdentifierHeader];
view.titleLabel.text = key;
view.selectedBtn.tag = section;
[view.selectedBtn addTarget:self action:@selector(headerButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
[view.tap addTarget:self action:@selector(headerTap:)];
if ([self.expendArray containsObject:key]) {
view.icon.transform = CGAffineTransformMakeRotation(M_PI_2);
}else {
view.icon.transform = CGAffineTransformIdentity;
}
return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = self.dataArray[indexPath.section];
NSArray *array = self.dataDic[key];
NSString *name = array[indexPath.row];
PlCustormCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifierCell forIndexPath:indexPath];
if (cell == nil) {
cell = [[PlCustormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseIdentifierCell];
}
if ([self.selectArray containsObject:name]) {
cell.selectedImage.selected = YES;
}else {
cell.selectedImage.selected = NO;
}
NSLog(@"tableView.visibleCells.count%lu",(unsigned long)tableView.visibleCells.count);
cell.backgroundColor = [UIColor lightTextColor];
cell.nameLabel.text = name;
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = self.dataArray[indexPath.section];
NSArray *array = self.dataDic[key];
NSString *name = array[indexPath.row];
NSLog(@"ng)indexPath.section%ld",(long)indexPath.section);
if ([self.selectArray containsObject:name]) {
[self.selectArray removeObject:name];
}else {
[self.selectArray addObject:name];
}
NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, indexPath.section)];
[self.tableView reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationNone];
[self.tableView reloadData];
}
#pragma mark - private methods
- (void)headerButtonOnClick:(UIButton *)button {
NSString *key = self.dataArray[button.tag];
NSArray *array = self.dataDic[key];
if (button.selected) {
[self.selectArray removeObjectsInArray:array];
}else {
[self.selectArray addObjectsFromArray:array];
}
button.selected = !button.selected;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)headerTap:(UITapGestureRecognizer *)tap {
HeaderView *view = (HeaderView *)tap.view;
NSString *key = view.titleLabel.text;
NSInteger index = [self.dataArray indexOfObject:key];
if ([self.expendArray containsObject:key]) {
[self.expendArray removeObject:key];
[UIView animateWithDuration:0.1 animations:^{
view.icon.transform = CGAffineTransformIdentity;
}];
}else {
[self.expendArray addObject:key];
[UIView animateWithDuration:0.1 animations:^{
view.icon.transform = CGAffineTransformMakeRotation(M_PI_2);
}];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - getters
- (NSMutableDictionary *)dataDic {
if (!_dataDic) {
_dataDic = [NSMutableDictionary dictionaryWithCapacity:0];
}
return _dataDic;
}
- (NSMutableArray *)expendArray {
if (!_expendArray) {
_expendArray = [NSMutableArray arrayWithCapacity:0];
}
return _expendArray;
}
- (NSMutableArray *)selectArray {
if (!_selectArray) {
_selectArray = [NSMutableArray arrayWithCapacity:0];
}
return _selectArray;
}