RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> { BOOL Close[15]; //用于存放每一组的收起展开状态 YES 是收起 NO是展开 UITableView *_tableView; } @property(nonatomic, retain)NSArray *data;
- (void)viewDidLoad { [super viewDidLoad]; //创建表视图 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; //设置代理 _tableView.delegate = self; _tableView.dataSource = self; _tableView.sectionHeaderHeight = 44; [self.view addSubview:_tableView]; //查找文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"]; _data = [[NSArray alloc] initWithContentsOfFile:path]; } /* _data数据存放的格式 [ [@"字体1",@"字体2",@"字体3",@"字体4",@"字体5"], [@"字体1",@"字体2",@"字体3"], [@"字体1",@"字体2"@"字体5"], [@"字体1",@"字体2",@"字体4",@"字体5"], .... ] */ #pragma mark - UITableView dataSource //设置表视图的组的个数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _data.count; } //设置每一组cell的个数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //取得对应组里面的元素 NSArray *arrary2D = _data[section]; BOOL isClose = Close[section]; if (isClose == NO) { return arrary2D.count; } return 0; } /* _data数据存放的格式 [ [@"字体1",@"字体2",@"字体3",@"字体4",@"字体5"], [@"字体1",@"字体2",@"字体3"], [@"字体1",@"字体2"@"字体5"], [@"字体1",@"字体2",@"字体4",@"字体5"], .... ] */ //创建cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *iden = @"cell110"; //从闲置池中去cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease]; } NSArray *arrary2D = [_data objectAtIndex:indexPath.section]; NSString *name = [arrary2D objectAtIndex:indexPath.row]; //给cell添加数据 cell.textLabel.text = name; cell.textLabel.font = [UIFont fontWithName:name size:17]; return cell; } //设置组的头视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { NSString *name = [NSString stringWithFormat:@"好友分组%d",section]; //创建按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //设置按钮的背景图片 [button setBackgroundImage:[UIImage imageNamed:@"tableCell_common"] forState:UIControlStateNormal]; //设置按钮的标题 [button setTitle:name forState:UIControlStateNormal]; button.tag = section; //设置按钮显示的标题颜色 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; return button; } //按钮的点击事件 - (void)buttonAction:(UIButton *)button { int section = button.tag; <strong>//将标示取反</strong> Close[section] = !Close[section]; //刷新单元格 // [_tableView reloadData]; //刷新特定的组 NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section]; [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade]; }
原文地址:http://blog.csdn.net/pengyuan_d/article/details/40615957