iOS实现类似QQ的好友列表,自由展开折叠(在原来TableView的基础上添加一个字典,一个Button)

//直接代码 只包含 折叠展开字典的处理搭建


#import
"CFViewController.h" @interface CFViewController ()<UITableViewDelegate,UITableViewDataSource> { UITableView *CFTableView; //数据 NSArray * CFOnearray; NSMutableArray *CFArray; NSArray *CFDetailArray; CGFloat HeightNormal; NSArray *arrayTitle;// NSArray * arraymodeltext;//内部分组Title CGFloat height0;//返回的高度 //按钮的状态相关 NSMutableDictionary * stateDict;//保存状态字典 } @end //chufang @implementation CFViewController - (void)viewDidLoad { [super viewDidLoad]; [self SetCustomBarWithLeftImage:@"ic_return" leftBtnString:nil titleText:@"处方" rightImageName:nil rightBtnSring:nil target:self]; [self loadData]; } -(void)loadData{ HeightNormal = IPHONEHIGHT(70); CFArray = [NSMutableArray array]; //包含状态的字典。 值为1 是展开状态 0收缩,,,以section为键 stateDict = [[NSMutableDictionary alloc]init]; arrayTitle = @[@"号: ",@"添加时间: ",@"诊: ",@"明细: ",@"金额: ",@"开: "]; [[GetUrlSession shareUrlconnection]connetion:[NSString stringWithFormat:@"%@?org_code=%@&clinic_id=%@",CFUrlStr,_jzmodel.org_code,_jzmodel.clinic_id] Haget:^(NSDictionary *data, NSError *error, NSHTTPURLResponse *response) { //CFOnearray 时下载的数据。如果为空了。就不搭建UI if (CFOnearray.count>0) { dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; [self creatUI]; }); }else{ dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; [self CreatNilUI]; }); } }]; } -(void)creatUI{ //创建tableView } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return CFOnearray.count; } //每一组 返回的行数。通过判断字典进行 看是否要展开 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSString * sectionNum = [NSString stringWithFormat:@"%ld",section]; NSString* state = [stateDict objectForKey:sectionNum]; if ([state isEqualToString:@"1"]) { return 6; }else{ return 0; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return cell; } //在透视图上添加一个button。进行监听点击事件 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIButton *backView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, IPHONEHIGHT(88))]; CFModel * model = CFOnearray[section]; backView.tag = 100+section; [backView addTarget: self action:@selector(tapDown:) forControlEvents:UIControlEventTouchUpInside]; backView.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(IPHONEWIDTH(28), IPHONEHIGHT(28),ScreenWidth- IPHONEWIDTH(100), IPHONEHIGHT(30))]; label.font = [UIFont systemFontOfSize:IPHONEWIDTH(30)]; label.textColor = shense; //组头大名字设定 label.text = [NSString stringWithFormat:@"%@(%@)",model.diagnosis,dateSring]; [backView addSubview:label]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(IPHONEWIDTH(667), IPHONEHIGHT(34), IPHONEWIDTH(27), IPHONEHIGHT(20))]; btn.tag = 1001+section; /////////////////////////判断是否展开折叠 改变button的状态 NSString * sectionNum = [NSString stringWithFormat:@"%ld",section]; NSString* state = [stateDict objectForKey:sectionNum]; if ([state isEqualToString:@"1"]) { btn.selected = YES; }else{ btn.selected = NO; } [btn setImage:[UIImage imageNamed:@"ic_home_hospital_close"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"ic_home_hospital_open"] forState:UIControlStateSelected]; [backView addSubview:btn]; //线 UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, backView.bottom-1, ScreenWidth, 1)]; grayView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"]; [backView addSubview:grayView]; return backView ; } //触发方法 改变字典的值 -(void)tapDown:(UIButton *)sender{ NSString * section = [NSString stringWithFormat:@"%ld",sender.tag-100]; NSString* state = [stateDict objectForKey:section]; if ([state isEqualToString:@"1"]) { state = @"0"; [stateDict setObject:state forKey:section]; }else{ state = @"1"; [stateDict setObject:state forKey:section]; } [CFTableView reloadData]; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return IPHONEHIGHT(88); } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 66; } //cell线到左边 -(void)viewDidLayoutSubviews { if ([CFTableView respondsToSelector:@selector(setSeparatorInset:)]) { [CFTableView setSeparatorInset:UIEdgeInsetsZero]; } if ([CFTableView respondsToSelector:@selector(setLayoutMargins:)]) { [CFTableView setLayoutMargins:UIEdgeInsetsZero]; } } -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{ if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setSeparatorInset:)]){ [cell setSeparatorInset:UIEdgeInsetsZero]; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
原文地址:https://www.cnblogs.com/xujiahui/p/6618022.html