@interface RootViewController : UIViewController{
BOOL Close[15]; //用于存放每一組的收起展開狀態(tài)? ? YES 是收起? NO是展開
UITableView *_tableView;
}
@property(nonatomic, retain)NSArray *data;
- (void)viewDidLoad{? [super viewDidLoad];? //創(chuàng)建表視圖? _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];? //設(shè)置代理? _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數(shù)據(jù)存放的格式 [ [@"字體1",@"字體2",@"字體3",@"字體4",@"字體5"], [@"字體1",@"字體2",@"字體3"], [@"字體1",@"字體2"@"字體5"], [@"字體1",@"字體2",@"字體4",@"字體5"], .... ] */#pragma mark - UITableView dataSource//設(shè)置表視圖的組的個數(shù)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {? return _data.count;}//設(shè)置每一組cell的個數(shù)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {? //取得對應(yīng)組里面的元素? NSArray *arrary2D = _data[section];? ? BOOL isClose = Close[section];? ? if (isClose == NO) {? ? return arrary2D.count;? }? ? return 0;? }/* _data數(shù)據(jù)存放的格式 [ [@"字體1",@"字體2",@"字體3",@"字體4",@"字體5"], [@"字體1",@"字體2",@"字體3"], [@"字體1",@"字體2"@"字體5"], [@"字體1",@"字體2",@"字體4",@"字體5"], .... ] *///創(chuàng)建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添加數(shù)據(jù)? cell.textLabel.text = name;? cell.textLabel.font = [UIFont fontWithName:name size:17];? ? return cell;? }//設(shè)置組的頭視圖- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {? NSString *name = [NSString stringWithFormat:@"好友分組%d",section];? ? //創(chuàng)建按鈕? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];? //設(shè)置按鈕的背景圖片? [button setBackgroundImage:[UIImage imageNamed:@"tableCell_common"] forState:UIControlStateNormal];? //設(shè)置按鈕的標(biāo)題? [button setTitle:name forState:UIControlStateNormal];? button.tag = section;? //設(shè)置按鈕顯示的標(biāo)題顏色? [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;//將標(biāo)示取反Close[section] = !Close[section];? ? //刷新單元格//[_tableView reloadData];? ? //刷新特定的組? NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];? ? [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];}