QQ列表的展開收起

@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];}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容