ios tableview分组
2013-08-13 21:59 甘超波 阅读(13105) 评论(0) 编辑 收藏 举报// // ViewController.m // UISCrollerviewTest // // Created by ganchaobo on 13-8-10. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import "ViewController.h" #import "MyCell.h" //rolad...方法 不管怎样都回调用数据源两个方法(1;numberOfSectionsInTableView //2 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //根据制定刷新 ,来刷新cell。 @interface ViewController (){ UITableView *_tableview; } @property(nonatomic,retain)NSMutableArray *data;//数据源 @property(nonatomic,retain)NSMutableDictionary *exp;//是否展开 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.data=[NSMutableArray array]; for (int i=0; i<3; i++) { NSString *str=[NSString stringWithFormat:@"it-%zi",i]; [self.data addObject:str]; } self.exp=[NSMutableDictionary dictionary]; // Do any additional setup after loading the view, typically from a nib. UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.separatorStyle=UITableViewCellSeparatorStyleNone; UIView *vv=[[UIView alloc] init]; vv.backgroundColor=[UIColor whiteColor]; tableview.backgroundView=vv; tableview.dataSource=self; tableview.delegate=self; [self.view addSubview:tableview]; _tableview=tableview; [tableview release]; } #pragma mark -数据源方法 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ NSLog(@"numberOfSectionsInTableView"); return 5; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"--section---%zi",section); NSString *indexsection=[NSString stringWithFormat:@"%zi",section]; BOOL b=[[self.exp objectForKey:indexsection] boolValue]; if ([self.exp objectForKey:indexsection] && b) { return self.data.count; } return 0; } -(void)click:(UIButton *)btn{ NSString *indexSection=[NSString stringWithFormat:@"%zi",btn.tag]; if ([self.exp objectForKey:indexSection]) {//如果有这个section BOOL b=[[self.exp objectForKey:indexSection] boolValue]; [self.exp removeAllObjects]; if(b){ [ self.exp setObject:[NSNumber numberWithBool:NO] forKey:indexSection]; } else { [ self.exp setObject:[NSNumber numberWithBool:YES] forKey:indexSection]; } } else{ [self.exp removeAllObjects]; [self.exp setObject:[NSNumber numberWithBool:YES] forKey:indexSection]; } // NSIndexSet *indexset=[NSIndexSet indexSetWithIndex:btn.tag]; // [_tableview reloadSections:indexset withRowAnimation:UITableViewRowAnimationFade]; [_tableview reloadData]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"--->section-->%zi---row-->%zi",indexPath.section,indexPath.row); static NSString *cellIdentify=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify]; if (cell==nil) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease]; } cell.textLabel.text=@"first button"; return cell; } #pragma mark -代理方法 //头部 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setTitle:@"SS" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; //btn.frame=CGRectMake(0, 0, 320, 80); btn.tag=section; return btn; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ NSLog(@"heightForHeaderInSection-->%zi",section); return 80; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"heightForRowAtIndexPath--->seciont-%zi--row---%zi",indexPath.section,indexPath.row); return 80; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"__%@",indexPath); } @end
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频