#import "ViewController.h" #define Ksmall 40.0f #define Klarge 80.0f #define KNoOpen @"NoOpen" @interface ViewController (){ int flag; } @property(nonatomic,retain)NSMutableDictionary *mydata; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.mydata=[NSMutableDictionary dictionary]; UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.delegate=self; tableview.dataSource=self; [self.view addSubview:tableview]; [tableview release]; // Do any additional setup after loading the view, typically from a nib. } //section -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 5; } //row -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"section--》%zi----row--->%zi",indexPath.section,indexPath.row); static NSString *cellindentify=@"mycell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellindentify]; if(cell==nil){ cell=[[[UITableViewCell alloc] init] autorelease]; } cell.textLabel.text=@"bb"; return cell; } #pragma mark -自定义分组的头部 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 40; } -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSLog(@"viewForHeaderInSection"); UIView *view=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)] autorelease]; UILabel *lb=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100-10, 40)]; lb.text=@"time"; lb.textAlignment=NSTextAlignmentLeft; [view addSubview:lb]; UILabel *lb1=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.view.bounds.size.width-100-10, view.bounds.size.height)]; lb1.textAlignment=NSTextAlignmentRight; lb1.text=@"right"; [view addSubview:lb1]; return view; } #pragma mark -设置CELL的高度 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row==0){ UITableViewCell *targetCell=[tableView cellForRowAtIndexPath:indexPath]; if(targetCell.frame.size.height==Ksmall+1){//折叠的情况 [self.mydata setObject:KNoOpen forKey:indexPath]; } else{ [self.mydata removeObjectForKey:indexPath]; } [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row==0){ NSString *isOpen= [self.mydata objectForKey:indexPath]; if ([isOpen isEqualToString:KNoOpen]) { return Klarge; } return Ksmall; } else return Klarge; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }