类似QQ的分组功能

实现代码很简单,就不多赘述了。

/** 数据源 */

@property (nonatomic,strong)NSMutableArray *sourceData;

/** 记录展开的section */

@property (nonatomic, strong) NSMutableArray *btnArr;


- (void)viewDidLoad {

[super viewDidLoad];

self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return self.sourceData.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if ([self.btnArr containsObject:[NSNumber numberWithInteger:section]])

{

//包含 展开的

return [self.sourceData[section][@"cellData"] count];

}

else

{

return 0;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *reusid = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusid];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusid];

}

cell.textLabel.text = self.sourceData[indexPath.section][@"cellData"][indexPath.row];

return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];

while (!header.contentView.subviews.count) {

//没有 需要添加一个button

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];

[header.contentView addSubview:btn];

btn.backgroundColor = [UIColor whiteColor];

[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

}

UIButton *btn = [header.contentView.subviews firstObject];

[btn setTitle:self.sourceData[section][@"groupTitle"] forState:UIControlStateNormal];

[btn addTarget:self action:@selector(btnClcik:) forControlEvents:UIControlEventTouchUpInside];

btn.tag = section;

return header;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 50;

}

-  (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

return 1;

}

- (void)btnClcik:(UIButton *)btn

{

if ([self.btnArr containsObject:[NSNumber numberWithInteger:btn.tag]])

{

//已经展开的

[self.btnArr removeObject:[NSNumber numberWithInteger:btn.tag]];

}else

{

[self.btnArr addObject:[NSNumber numberWithInteger:btn.tag]];

}

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

}

/**

*  数据源的构造 字典数组

*

*  @return <#return value description#>

*/

- (NSMutableArray *)sourceData

{

if (!_sourceData) {

_sourceData = [[NSMutableArray alloc]init];

for (NSInteger i  = 0 ; i < 5 ; i ++ ) {

NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];

NSArray *arr = [[NSArray alloc]initWithObjects:@"武松",@"西门庆",@"潘金莲",@"陈翔", nil];

dic[@"cellData"] = arr;

NSString *title = [NSString stringWithFormat:@"第%zd组",i];

dic[@"groupTitle"] = title;

[_sourceData addObject:dic];

}

}

return _sourceData;

}

- (NSMutableArray *)btnArr

{

if (!_btnArr) {

_btnArr = [[NSMutableArray alloc]init];

}

return _btnArr;

}

你可能感兴趣的:(类似QQ的分组功能)