可折叠TableViewCell


- (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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容