iOS中Cell的开展和收起

  • 时间:2018-09-14 23:10 作者:MR小锦 来源:MR小锦 阅读:702
  • 扫一扫,手机访问
摘要:首先,先上图,让大家看看效果Simulator Screen Shot 2017年5月22日 下午1.43.37.png相信大家对于TableViewd数据的设置都熟习,这方面就不多说的,重点的还是来看:1.如何实现cell的开展和收起的效果- (void)tableView:(UITableVie

首先,先上图,让大家看看效果

Simulator Screen Shot 2017年5月22日 下午1.43.37.png

相信大家对于TableViewd数据的设置都熟习,这方面就不多说的,重点的还是来看:

1.如何实现cell的开展和收起的效果

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];            currentRow = indexPath.row;        NSDictionary *sectionDic = self.dataSource[indexPath.section];    NSArray *cellArray = sectionDic[@"sub"];        //cell当前的数据    NSDictionary *cellData = cellArray[indexPath.row];        NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];    CellModel *chapterModel = [self.cellOpen valueForKey:key];        chapterModel.isShow = !chapterModel.isShow;        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];}

当使用户点击到某一个cell时候,需要判断cell能否是开展状态,假如张开或者者收起就调使用

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

让cell的section能够重新加载刷新;

2.如何添加cell的某一个Section的row

2.1设置好对使用能否开展row的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    NSDictionary *sectionDic = self.dataSource[indexPath.section];    NSArray *cellArray = sectionDic[@"sub"];    //cell当前的数据    NSDictionary *cellData = cellArray[indexPath.row];        NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];    CellModel *model = [self.cellOpen valueForKey:key];    if (model.isShow) {        return (model.pois.count+1)*60;    } else {        return 60;    }    }
2.2 返回对应的section的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    BOOL sectionStates = [self.sectionOpen[section] boolValue];        if(sectionStates)    {        //数据决定显示多少行cell        NSDictionary *sectionDict = self.dataSource[section];        //section决定cell的数据        NSArray *cellArray = sectionDict[@"sub"];        return cellArray.count;            }    else    {        //section是收起的时候        return 0;    }}
好了,说了那么多,预计大家还是喜欢看demo,以下是demo的链接: xiaojin1123/SectionOpenAndClose.git
欢迎大家提出自己看法!
  • 全部评论(0)
最新发布的资讯信息
【系统环境|】在Qt中如何设置窗体的背景图片(2025-10-29 20:27)
【系统环境|】无声无名 | 杨·罗威斯终身回顾展(2025-10-29 20:26)
【系统环境|】从零开始学Qt(22):QSS详解(3)- 盒子模型(2025-10-29 20:25)
【系统环境|】Quarkus vs Spring Boot 集成 RabbitMQ 谁更香?(2025-10-29 20:24)
【系统环境|】RabbitMQ相关概念及代码示例(2025-10-29 20:24)
【系统环境|】Spring Boot + RabbitMQ:轻松掌握五种基本工作模式(2025-10-29 20:23)
【系统环境|】一篇文章带你彻底玩转-RabbitMQ(2025-10-29 20:22)
【系统环境|】私有云平台搭建——史上最详细(2025-10-29 20:21)
【系统环境|】RabbitMQ最全详解(万字图文总结)(2025-10-29 20:20)
【系统环境|】.Net/C#全网最火RabbitMQ操作【强烈推荐】(2025-10-29 20:20)
手机二维码手机访问领取大礼包
返回顶部