几行代码实现点击UITableView展开cell

/**选中的行*/

@property (nonatomic, assign) NSInteger selectedRow;

/**记录被打开的行号*/

@property (nonatomic, strong) NSMutableArray *open;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

RCLog(@"-selectedRow--%zd--indexPath.row)-%zd",self.selectedRow,indexPath.row);

if(!self.selectedRow){

//第一次点击都要加进去

[self.open addObject:[NSString stringWithFormat:@"%zd",indexPath.row]];

}else{

if ([self.open containsObject:[NSString stringWithFormat:@"%zd",indexPath.row]]) {

//包含,说明之前已经点击过,这次点击的跟上一次是一样的,从打开数组中移除

[self.open removeObject:[NSString stringWithFormat:@"%zd",indexPath.row]];

}else{

//不包含,说明这行时第一次点击,直接加进去就可以了

[self.open addObject:[NSString stringWithFormat:@"%zd",indexPath.row]];

}

}

//记录点击的行号

self.selectedRow = indexPath.row;

//刷新点击的行

[self.table reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *reusid = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusid];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusid];

}

if ([self.open containsObject:[NSString stringWithFormat:@"%zd",indexPath.row]]) {

//打开之后

cell.textLabel.text = @"快点关上,我要死啦";

}else{

//正常状态下

cell.textLabel.text = @"点击可以打开我噢";

}

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([self.open containsObject:[NSString stringWithFormat:@"%zd",indexPath.row]]) {

//打开的时候cell的高度

return 100;

}else{

return 40;

}

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,452评论 9 5
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,918评论 2 10
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 556评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 944评论 0 0