博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[原]列表的二级展开功能。在原生UITableView上扩展实现。
阅读量:6243 次
发布时间:2019-06-22

本文共 4209 字,大约阅读时间需要 14 分钟。

  还是直接贴代码了。

第一步:

@interface TodoViewController (){    //声明一个数组 存放cell的信息    NSMutableArray *grouparr;}@end

 

第二步:

//将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];}

  

第三步:

-(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;    }

  

第四步:

-(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];        }    }}

  

搞定收工!

转载于:https://www.cnblogs.com/pandas/p/4287831.html

你可能感兴趣的文章
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
我的友情链接
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
windows查看端口占用
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
App 卸载记录
查看>>
JavaScript变量和作用域
查看>>
开源SIP服务器加密软件NethidPro升级
查看>>
Apache Pulsar中的地域复制,第1篇:概念和功能
查看>>
python pip install 出现 OSError: [Errno 1] Operation not permitted
查看>>
从源码分析scrollTo、scrollBy、Scroller方法的区别和作用
查看>>
南京大学周志华教授当选欧洲科学院外籍院士
查看>>
计算机网络与Internet应用
查看>>
oracle在线迁移同步数据,数据库报错
查看>>
linux性能剖析工具
查看>>
flutter中的异步
查看>>
计算机高手也不能编出俄罗斯方块——计算机达人成长之路(16)
查看>>