[原]列表的二级展开功能。在原生UITableView上扩展实现。
还是直接贴代码了。
第一步:
1 2 3 4 5 6 | @interface TodoViewController () { //声明一个数组 存放cell的信息 NSMutableArray *grouparr; } @end |
第二步:
1 2 3 4 5 6 7 8 9 | //将cell的状态存入数组中 -( void )initDataSource { NSMutableDictionary *nameAndStateDic1 = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "TodoCell" ,@ "cell" ,@ "NO" ,@ "state" , nil ]; NSMutableDictionary *nameAndStateDic2 = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "TodoCell" ,@ "cell" ,@ "NO" ,@ "state" , nil ]; NSMutableDictionary *nameAndStateDic3 = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "TodoCell" ,@ "cell" ,@ "NO" ,@ "state" , nil ]; NSMutableDictionary *nameAndStateDic4 = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "TodoCell" ,@ "cell" ,@ "NO" ,@ "state" , nil ]; grouparr = [[ NSMutableArray alloc] initWithObjects:nameAndStateDic1,nameAndStateDic2,nameAndStateDic3, nameAndStateDic4, nil ]; } |
第三步:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | -( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section { return grouparr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath { if ([grouparr[indexPath.row][@ "cell" ] isEqualToString:@ "TodoCell" ]) { static NSString *cellID = @ "cell" ; TodoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[TodoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } NSData *data = dataArray[indexPath.row]; [cell bindData:data]; return cell; } else { static NSString *CellIdentifier = @ "AttachedCell" ; AttachedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil ) { cell = [[AttachedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } return nil ; } |
第四步:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | -( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath { //点击cell后 改变cell的颜色 渐变 [_tableView deselectRowAtIndexPath:indexPath animated: YES ]; NSIndexPath *path = nil ; if ([grouparr[indexPath.row][@ "cell" ] isEqualToString:@ "MainCell" ]) { if ([grouparr[indexPath.row][@ "cell" ] isEqualToString:@ "MainCell" ]) { path = [ NSIndexPath indexPathForItem:(indexPath.row+1) inSection:indexPath.section]; } else if ([grouparr[indexPath.row][@ "cell" ] isEqualToString:@ "AttachedCell" ]) { path = indexPath; } NSLog (@ "现在是第%ld行" ,indexPath.row); if ([grouparr[indexPath.row][@ "state" ] boolValue] ) { // 关闭附加cell NSMutableDictionary *dd = grouparr[indexPath.row]; NSString *name = dd[@ "name" ]; NSMutableDictionary *nameAndStateDic = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "MainCell" ,@ "cell" ,name,@ "name" ,@ "NO" ,@ "state" , nil ]; grouparr[(path.row-1)] = nameAndStateDic; [grouparr removeObjectAtIndex:path.row]; NSLog (@ "MainCell's grouparr:%@" ,grouparr); [_tableView beginUpdates]; [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle]; [_tableView endUpdates]; } else { // 打开附加cell NSMutableDictionary *dd = grouparr[indexPath.row]; NSString *name = dd[@ "name" ]; NSMutableDictionary *nameAndStateDic = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "MainCell" ,@ "cell" ,name,@ "name" ,@ "YES" ,@ "state" , nil ]; grouparr[(path.row-1)] = nameAndStateDic; NSMutableDictionary *nameAndStateDic1 = [ NSMutableDictionary dictionaryWithObjectsAndKeys:@ "AttachedCell" ,@ "cell" ,@ "YES" ,@ "state" , nil ]; [grouparr insertObject:nameAndStateDic1 atIndex:path.row]; NSLog (@ "AttachedCell's grouparr:%@" ,grouparr); [_tableView beginUpdates]; [_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle]; [_tableView endUpdates]; } } } |
搞定收工!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析