折叠式cell,点击展开可以查看所有cell

/** 数据源  */

@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);

}

}

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,092评论 3 38
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,770评论 1 9
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,453评论 9 5
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 569评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 961评论 0 0