/** 数据源 */
@property (nonatomic, strong) NSMutableArray *dataSource;
/** 记录cell的是否折叠的状态 */
@property (nonatomic, strong) NSMutableArray *ledArr;
- (NSMutableArray *)dataSource
{
if (!_dataSource) {
_dataSource = [[NSMutableArray alloc]init];
for (int i =0 ;i <= 10 ;i ++ ) {
[_dataSource addObject:@"dd"];
}
}
return _dataSource;
}
- (NSMutableArray *)ledArr
{
if (!_ledArr) {
_ledArr = [[NSMutableArray alloc]init];
for (int i = 0 ; i <= 10 ; i ++) {
/**
* 这里的YES是代表每个section的cell都需要折叠
* NO的话是代表点击了展开或者cell本身比较少无需折叠
*/
NSNumber *led = [NSNumber numberWithBool:YES];
[_ledArr addObject:led];
}
}
return _ledArr;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.ledArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/**
* 取出section对应的状态
* YES代表需要折叠
* NO代表显示全部
*/
BOOL led = [self.ledArr[section] boolValue];
if (led)
{
return 4;
}
else
{
return self.dataSource.count;
}
}
/**
* 这里使用两种从xib加载的cell
* 简单演示了如何实现
*
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
HeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadTableViewCell"];
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"HeadTableViewCell" owner:nil options:nil] firstObject];
}
return cell;
}
else
{
ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentTableViewCell"];
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ContentTableViewCell" owner:nil options:nil] firstObject];
}
if(self.dataSource.count > 3)
{
if (indexPath.row == 3) {
BOOL led = [self.ledArr[indexPath.section] boolValue];
if (led)
{
cell.contentLabel.text = @"展开";
}else
{
cell.contentLabel.text = @"这里是内容";
}
}
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL led = self.ledArr[indexPath.section];
if (led) {
if (indexPath.row == 3)
{
/**
* 代表点击了展开
* 将之前的状态移除
* 添加为NO的状态
* 而后刷新表格
*/
NSNumber *newLed = [NSNumber numberWithBool:NO];
[self.ledArr removeObjectAtIndex:indexPath.section];
[self.ledArr insertObject:newLed atIndex:indexPath.section];
[self.table reloadData];
}else
{
NSLog(@"section-%zd - row --%zd",indexPath.section,indexPath.row);
}
}
}