美文网首页
可折叠TableViewCell

可折叠TableViewCell

作者: 小破孩丫 | 来源:发表于2016-01-25 13:19 被阅读109次

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

相关文章

网友评论

      本文标题:可折叠TableViewCell

      本文链接:https://www.haomeiwen.com/subject/betdkttx.html