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();
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>) { 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:@""]) { return ; }else{ return ;
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return cell;
}
//在透视图上添加一个button。进行监听点击事件
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *backView = [[UIButton alloc]initWithFrame:CGRectMake(, , ScreenWidth, IPHONEHIGHT())]; CFModel * model = CFOnearray[section]; backView.tag = +section;
[backView addTarget: self action:@selector(tapDown:) forControlEvents:UIControlEventTouchUpInside];
backView.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(IPHONEWIDTH(), IPHONEHIGHT(),ScreenWidth- IPHONEWIDTH(), IPHONEHIGHT())]; label.font = [UIFont systemFontOfSize:IPHONEWIDTH()];
label.textColor = shense; //组头大名字设定
label.text = [NSString stringWithFormat:@"%@(%@)",model.diagnosis,dateSring];
[backView addSubview:label]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(IPHONEWIDTH(), IPHONEHIGHT(), IPHONEWIDTH(), IPHONEHIGHT())];
btn.tag = +section; /////////////////////////判断是否展开折叠 改变button的状态
NSString * sectionNum = [NSString stringWithFormat:@"%ld",section]; NSString* state = [stateDict objectForKey:sectionNum]; if ([state isEqualToString:@""]) { 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(, backView.bottom-, ScreenWidth, )];
grayView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
[backView addSubview:grayView];
return backView ;
}
//触发方法 改变字典的值
-(void)tapDown:(UIButton *)sender{ NSString * section = [NSString stringWithFormat:@"%ld",sender.tag-]; NSString* state = [stateDict objectForKey:section]; if ([state isEqualToString:@""]) { state = @"";
[stateDict setObject:state forKey:section]; }else{ state = @"";
[stateDict setObject:state forKey:section]; } [CFTableView reloadData]; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return IPHONEHIGHT();
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return ;
} //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
上一篇:UVA11039-Building designing


下一篇:net core体系-Standard-1概述