iOS tableView点击分区下拉单元格特效

来源:互联网 发布:java web创意项目 编辑:程序博客网 时间:2024/05/17 04:25

#import "MGMineMenuVc.h"

#import "MGSubSelectVc.h"

#import "HeaderView.h"//   此为自定义的分区视图

#import "PlCustormCell.h" //自定义单元格


staticNSString * const ReuseIdentifierHeader =@"header";

staticNSString * 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 {

    [superviewDidLoad];

   self.title =@"筛选";

    self.isPieceSelected =NO;

    

    UIBarButtonItem *cancelBarItem = [[UIBarButtonItemalloc] initWithTitle:@"取消"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(cancelAction)];

    self.navigationItem.leftBarButtonItem = cancelBarItem;

    

    

     UIBarButtonItem *SureBarItem = [[UIBarButtonItemalloc] initWithTitle:@"确定"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(cancelAction)];

    self.navigationItem.leftBarButtonItem = cancelBarItem;

    self.navigationItem.rightBarButtonItem = SureBarItem;

    

    [selfloadData];

    [selfinitTableView];

    

   self.rightGesture = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(rightGestureRec:)];

    [self.viewaddGestureRecognizer:self.rightGesture];

    self.rightGesture.direction = UISwipeGestureRecognizerDirectionRight;


}

-(void)rightGestureRec:(UISwipeGestureRecognizer *)sender

{

    [selfcancelAction];

}



-(void)loadData

{

    NSString *path  = [[NSBundlemainBundle] pathForResource:@"role"ofType:@"plist"];

    NSMutableArray *wai = [[NSMutableArrayalloc] initWithContentsOfFile:path];

   for (int i =0; i < wai.count; i ++)

    {

       NSArray *list = [wai[i] objectForKey:@"list"];

        NSMutableArray *array = [NSMutableArrayarrayWithCapacity:0];

       for (int j =0; j < list.count; j ++) {

            [arrayaddObject:[list objectAtIndex:j]];

            

        }

        [self.dataDicsetObject:array forKey:[wai[i]objectForKey:@"name"]];

    }

    self.dataArray = [self.dataDicallKeys];

}

-(void)initTableView

{

    self.tableView.tableFooterView = [UIViewnew];

    

    [self.tableViewsetTableFooterView:[[UIViewalloc] initWithFrame:CGRectZero]];

    

    self.tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0,Screen_weight-kMGLeftSpace,Screen_height)style:UITableViewStyleGrouped];

    [self.tableViewregisterClass:[HeaderViewclass] forHeaderFooterViewReuseIdentifier:ReuseIdentifierHeader];

    [self.tableViewregisterClass:[PlCustormCellclass] forCellReuseIdentifier:ReuseIdentifierCell];

   self.tableView.delegate =self;

   self.tableView.dataSource =self;

    [self.viewaddSubview: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.expendArraycontainsObject: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 = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:ReuseIdentifierHeader];

    view.titleLabel.text = key;

    view.selectedBtn.tag = section;

    

    [view.selectedBtnaddTarget:selfaction:@selector(headerButtonOnClick:)forControlEvents:UIControlEventTouchUpInside];

    

    [view.tapaddTarget:selfaction:@selector(headerTap:)];

    

   if ([self.expendArraycontainsObject: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 = [tableViewdequeueReusableCellWithIdentifier:ReuseIdentifierCellforIndexPath:indexPath];

    

   if (cell == nil) {

        cell  = [[PlCustormCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ReuseIdentifierCell];

    }

    

   if ([self.selectArraycontainsObject:name]) {

        cell.selectedImage.selected =YES;

    }else {

        cell.selectedImage.selected =NO;

    }


    NSLog(@"tableView.visibleCells.count%lu",(unsignedlong)tableView.visibleCells.count);

    cell.backgroundColor = [UIColorlightTextColor];

    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.selectArraycontainsObject:name]) {

        [self.selectArrayremoveObject:name];

    }else {

        [self.selectArrayaddObject:name];

    }

    NSIndexSet *sectionIndexSet = [NSIndexSetindexSetWithIndexesInRange:NSMakeRange(0, indexPath.section)];

    [self.tableViewreloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationNone];

    [self.tableViewreloadData];

}



#pragma mark - private methods


- (void)headerButtonOnClick:(UIButton *)button {

   NSString *key = self.dataArray[button.tag];

   NSArray *array = self.dataDic[key];

    

   if (button.selected) {

        [self.selectArrayremoveObjectsInArray:array];

    }else {

        [self.selectArrayaddObjectsFromArray:array];

    }

    

    button.selected = !button.selected;

    

    [self.tableViewreloadSections:[NSIndexSetindexSetWithIndex:button.tag]withRowAnimation:UITableViewRowAnimationNone];

}


- (void)headerTap:(UITapGestureRecognizer *)tap {

   HeaderView *view = (HeaderView *)tap.view;

   NSString *key = view.titleLabel.text;

   NSInteger index = [self.dataArrayindexOfObject:key];

    

   if ([self.expendArraycontainsObject:key]) {

        [self.expendArrayremoveObject:key];

        [UIViewanimateWithDuration:0.1animations:^{

            view.icon.transform =CGAffineTransformIdentity;

        }];

    }else {

        [self.expendArrayaddObject:key];

        [UIViewanimateWithDuration:0.1animations:^{

            view.icon.transform =CGAffineTransformMakeRotation(M_PI_2);

        }];

    }

    

    [self.tableViewreloadSections:[NSIndexSetindexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];


}



#pragma mark - getters


- (NSMutableDictionary *)dataDic {

   if (!_dataDic) {

        _dataDic = [NSMutableDictionarydictionaryWithCapacity:0];

    }

    return_dataDic;

}


- (NSMutableArray *)expendArray {

    if (!_expendArray) {

        _expendArray = [NSMutableArrayarrayWithCapacity:0];

    }

    return_expendArray;

}


- (NSMutableArray *)selectArray {

    if (!_selectArray) {

        _selectArray = [NSMutableArrayarrayWithCapacity:0];

    }

    return_selectArray;

}




0 0
原创粉丝点击