[原]列表的二级展开功能。在原生UITableView上扩展实现。

  还是直接贴代码了。

第一步:

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

  

搞定收工!

时间: 2024-09-20 19:30:34

[原]列表的二级展开功能。在原生UITableView上扩展实现。的相关文章

实现列表二级展开/收起/选择

代码地址如下:http://www.demodashi.com/demo/12628.html 1.先看效果图 2.实现原理 1)通过对UITableView进行分组来实现展开收起的功能: 2)通过cell的model和组model中的是否选中和是否展开的标记字段来记录并实现展开/收起/选择的功能: 3.代码实现 1)创建cell的model类并添加如下属性: // 标记是否选中 @property (nonatomic, assign) BOOL isSelect; // 文字 @proper

android列表收缩与展开仿QQ好友列表(非常详细,附源码)

好友QQ列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,下面直接上效果图以及源代码~! ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子

利用swipelistview完成qq聊天列表右滑删除功能

转载请注明出处:http://blog.csdn.net/harryweasley/article/details/41413547 前言:前段时间,由于自己比较悠闲,没有什么工作上的事情所做,所以当时就想,何不做一下qq聊天列表的item右滑出现后面的视图,然后我就开始着手自己做,之后无意间发现了可以利用一个github开源项目swiplistview完成,但是当我查看那些swiplistview的相关文档时,很不全面,github官网上下载的和之前他们写的文档已经有所不同,所以在这里我就重新

python---用链表结构实现有序和无序列表的几个功能

只是看看套路,没有深入练习. 如果真要自己写,可以基于此类. 但其实,在普通使用中,这样实现的性能,并没有python原生的列表性能好. 因为python原生列表的功能,是基于数组作扩展实现的. # coding: utf-8 class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self

Android 实现用户列表信息滑动删除功能和选择删除功能

在项目开发过程中,常常需要对用户列表的信息进行删除的操作.Android中常用的删除操作方式有两种 ,一种就是类似微信的滑动出现删除按钮方式,还有一种是通过CheckBox进行选择,然后通过按钮进行删除的方式.本来的实例集成上述的两种操作方式来实现用户列表删除的效果. 设计思路:在适配器类MyAdapter一个滑动删除按钮显示或隐藏的Map,一个用于CheckBox是否选中的Map和一个与MainAcitivyt进行数据交互的接口ContentsDeleteListener,同时该接口包含两个方

Python List sort()方法-用于对原列表进行排序

描述 sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数. 语法 sort()方法语法: list.sort([func]) 参数 func -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序. 返回值 该方法没有返回值,但是会对列表的对象进行排序. 实例 以下实例展示了 sort()函数的使用方法: #!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc', 'xyz']; aList.sort(); p

sharepoint 2016 学习系列篇(16)-自定义列表应用篇-(5)列表的快速编辑功能

平时我们都是通过新增按钮进行添加数据,其实还有一种更加方便使用,而且快捷的方式录入数据,就是列表的快速编辑功能,这里我们来演示下,这个功能的好用之处. 打开前面创建好的用户信息表,点击快速编辑(edit)按钮, 进入快速编辑模式,然后选中一行数据,当然如果有多行数据,也可以选择多行,接下来用键盘快捷键Ctrl+C进行复制 会弹出一个提示框,点击允许访问 选中列表的空白行 用快捷键Ctrl+V进行粘贴. 我们发现,数据已经新增进去了,这里还可以对每个单元格进行修改,和Excel的操作方式很相识,录

python列表的方法(改变原列表)

xxx.append()      末尾添加一个元素效果图: 代码: # append() 末尾添加一个元素 my_list = ['1','2','3','4'] print('原列表:',my_list) my_list.append('five') print('现列表:',my_list) xxx.insert()     向指定位置插入元素效果图: 代码: # insert() 向指定位置插入元素 my_list = ['1','2','3','4'] print('指定插入前',my

微信小程序中悬浮窗功能的实现(主要探讨和解决在原生组件上的拖动)

问题场景 所谓悬浮窗就是图中微信图标的按钮,采用fixed定位,可拖动和点击. 这算是一个比较常见的实现场景了. 为什么要用cover-view做悬浮窗?原生组件出来背锅了~ 最初我做悬浮窗用的不是cover-view,而是view. 这是简化的代码结构: index.wxml: <view class="move-view" style=" top:{{top}}px;left:{{left}}px;" bindtap="goToHome"