- (void)viewDidLoad {
[super viewDidLoad];
_tittleArray = [NSMutableArray arrayWithObjects:@"标题1",@"标题2",@"标题3",@"标题4", nil];
_rowInSectionArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
_selectArray = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _tittleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([_selectArray[section] isEqualToString:@"1"]) {
return [_rowInSectionArray[section] integerValue];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = _tittleArray[indexPath.section];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
sectionView.backgroundColor = [UIColor purpleColor];
UIButton *secBtn = [UIButton buttonWithType:UIButtonTypeCustom];
secBtn.frame = sectionView.frame;
[secBtn setTitle:[_tittleArray objectAtIndex:section] forState:UIControlStateNormal];
[secBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
secBtn.tag = 1000 + section;
[sectionView addSubview:secBtn];
return sectionView;
}
-(void)buttonAction:(UIButton *)button{
if ([_selectArray[button.tag -1000] isEqualToString:@"0"]) {
_selectArray[button.tag - 1000] = @"1";
// [_selectArray replaceObjectAtIndex:button.tag -1000 withObject:@"1"];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag -1000] withRowAnimation:UITableViewRowAnimationFade];
} else{
_selectArray[button.tag - 1000] = @"0";
// [_selectArray replaceObjectAtIndex:button.tag -1000 withObject:@"1"];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag -1000] withRowAnimation:UITableViewRowAnimationFade];
}
}
@end
网友评论