/** 數(shù)據(jù)源? */
@property (nonatomic, strong) NSMutableArray *dataSource;
/** 記錄cell的是否折疊的狀態(tài) */
@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是代表每個(gè)section的cell都需要折疊
*? NO的話是代表點(diǎn)擊了展開或者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對應(yīng)的狀態(tài)
*? YES代表需要折疊
*? NO代表顯示全部
*/
BOOL led = [self.ledArr[section] boolValue];
if (led)
{
return 4;
}
else
{
return self.dataSource.count;
}
}
/**
*? 這里使用兩種從xib加載的cell
*? 簡單演示了如何實(shí)現(xiàn)
*
*/
- (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 = @"這里是內(nèi)容";
}
}
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL led = self.ledArr[indexPath.section];
if (led) {
if (indexPath.row == 3)
{
/**
*? 代表點(diǎn)擊了展開
*? 將之前的狀態(tài)移除
*? 添加為NO的狀態(tài)
*? 而后刷新表格
*/
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);
}
}
}