//从服务器请求回数据
if ([run_num intValue] == 1) {
NSArray *arr = [dictionary valueForKey:@"key"];
[self.Arrdata removeAllObjects];
for(NSDictionary *dic in arr){
NSMutableDictionary *mudic=[NSMutableDictionary dictionaryWithDictionary:dic];
[mudic setObject:[NSNumber numberWithBool:NO] forKey:@"open"];
[self.Arrdata addObject:mudic];
}
[self.tableview reloadData];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.Arrdata.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDictionary *dic =[self.Arrdata objectAtIndex:section];
return [dic valueForKey:@"name"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *dic=self.Arrdata[section];
if([[dic objectForKey:@"open"] boolValue]){
//展开
return [(NSArray *)[dic objectForKey:@"item"] count];
}else{
//关闭
return 0;
}
}
-(float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSDictionary *dic =[self.Arrdata objectAtIndex:section];
UIButton *button=[MyControl createButtonWithFrame:CGRectMake(0, 0, ScrrenWidth, 50) ImageName:nil Target:self Action:@selector(buttonClick:) Title:[dic valueForKey:@"name"]];
button.tag=section+300;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.contentMode=UIViewContentModeLeft;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor=[UIColor whiteColor];
UIImageView *rightView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Jright"] highlightedImage:[UIImage imageNamed:@"Jbottom"]];
BOOL isopen=[[dic objectForKey:@"open"] boolValue];
rightView.highlighted=isopen;
rightView.frame=CGRectMake(ScrrenWidth-30, 20, 15, 15);
rightView.tag=998;
[button addSubview:rightView];
return button;
}
#pragma mark---------头部师徒点击
-(void)buttonClick:(UIButton *)btn
{
NSDictionary *dic =[self.Arrdata objectAtIndex:btn.tag-300];
BOOL isopen=[[dic objectForKey:@"open"] boolValue];
UIImageView *rightView=(UIImageView *)[btn viewWithTag:998];
rightView.highlighted=!isopen;
[dic setValue:[NSNumber numberWithBool:!isopen] forKey:@"open"];
[self.tableview reloadSections:[NSIndexSet indexSetWithIndex:btn.tag-300] withRowAnimation:UITableViewRowAnimationFade];
}
// 设置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strcelltag =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strcelltag];
if (cell== nil) {
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"SelectQuestionCell" owner:nil options:nil];
cell = [arr objectAtIndex:0];
}
UILabel *labtitle = (UILabel *)[cell viewWithTag:21];
NSDictionary *dict = [self.Arrdata objectAtIndex:indexPath.section];
NSArray *arr = [dict valueForKey:@"item"];
labtitle.text = [arr objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableview deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *dict = [self.Arrdata objectAtIndex:indexPath.section];
NSArray *arr = [dict valueForKey:@"item"];
HomeSearchListViewController *vc=[HomeSearchListViewController new];
vc.query_type=@"question";
vc.title=[arr objectAtIndex:indexPath.row];
vc.searchStr = vc.title;
[self.navigationController pushViewController:vc animated:YES];
}
原文地址:http://blog.csdn.net/heyachaodeios/article/details/45721533