之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个cell同时展开,只能一个一个的展开。

我就尝试用用数组记录展开的标记的方法,功能实现了,

直接上代码:

  1. //
  2. // ViewController.m
  3. // 折叠tableview-Test
  4. //
  5. // Created by abc on 16/7/26.
  6. // Copyright © 2016年 LiuWenqiang. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. #define ALLWIDTH [UIScreen mainScreen].bounds.size.width
  12. #define ALLHEIGHT [UIScreen mainScreen].bounds.size.height
  13.  
  14. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
  15. {
  16. UITableView*tableview;
  17. NSArray *dataArr;
  18. NSMutableArray *isOpenArr;
  19.  
  20. }
  21. @end
  22.  
  23. @implementation ViewController
  24.  
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27.  
  28. tableview = [[UITableView alloc]initWithFrame:CGRectMake(, , ALLWIDTH, ALLHEIGHT-)];
  29. tableview.delegate =self;
  30. tableview.dataSource = self;
  31. tableview.tableFooterView = [[UIView alloc]init];
  32. [self.view addSubview:tableview];
  33.  
  34. dataArr = [NSArray array];
  35. dataArr = @[@"呵呵",@"哈哈",@"嗯嗯",@"呃呃",@"很好"];
  36.  
  37. isOpenArr = [NSMutableArray array];
  38.  
  39. for (int i =; i< dataArr.count; i++) {
  40.  
  41. [isOpenArr addObject:@""];
  42. }
  43.  
  44. }
  45. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  46. {
  47. return ;
  48. }
  49. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  50. {
  51. if([isOpenArr[section] isEqualToString:@""])
  52. {
  53. return dataArr.count;
  54. }else{
  55. return ;
  56. }
  57. }
  58. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. NSString *cellstr = @"cell";
  61. UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr];
  62. if (!cell) {
  63. cell = [tableview dequeueReusableCellWithIdentifier:cellstr forIndexPath:indexPath];
  64. }
  65.  
  66. cell.textLabel.text = dataArr[indexPath.row];
  67.  
  68. return cell;
  69.  
  70. }
  71.  
  72. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  73. {
  74. return ;
  75. }
  76.  
  77. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  78. {
  79. UIView *view =[[UIView alloc]init];
  80. UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, ,ALLWIDTH, )];
  81. lable.text = [NSString stringWithFormat:@"------------第%ld组---------",(long)section];
  82. lable.textColor = [UIColor redColor];
  83. [view addSubview:lable];
  84.  
  85. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topgesture:)];
  86. [view addGestureRecognizer:tap];
  87.  
  88. view.tag = section;
  89. return view;
  90.  
  91. }
  92. //在组头上添加点击手势
  93. -(void)topgesture:(UITapGestureRecognizer*)tap
  94. {
  95. NSInteger index = tap.view.tag;
  96.  
  97. if ([isOpenArr[index] isEqualToString:@""]) {
  98.  
  99. [isOpenArr replaceObjectAtIndex:index withObject:@""];
  100.  
  101. }else{
  102. [isOpenArr replaceObjectAtIndex:index withObject:@""];
  103.  
  104. }
  105. [tableview reloadData];
  106.  
  107. }
  108.  
  109. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. [tableview deselectRowAtIndexPath:indexPath animated:YES];
  112. NSLog(@"------================--%ld-----",indexPath.row);
  113. }
  114.  
  115. @end

自己最近几天正在学习swifit,于是就试着用swift 写了一遍,

  1. //
  2. // ViewController.swift
  3. // 测试--Swift--Test
  4. //
  5. // Created by abc on 16/7/26.
  6. // Copyright © 2016年 LiuWenqiang. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  12.  
  13. let ALLwidth = UIScreen.mainScreen().bounds.width
  14. let ALLheight = UIScreen.mainScreen().bounds.height
  15.  
  16. var tableview:UITableView = UITableView()
  17. var DataArr: NSMutableArray = NSMutableArray()
  18.  
  19. var selectindex:NSMutableArray = NSMutableArray()
  20.  
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23.  
  24. tableview.frame = CGRectMake(, , ALLwidth, ALLheight)
  25. tableview.delegate = self
  26. tableview.dataSource = self
  27. self.view.addSubview(tableview)
  28.  
  29. DataArr = ["第一行","第二行","第三行","第四行"]
  30.  
  31. for _ in DataArr {
  32.  
  33. selectindex.addObject("")
  34. }
  35.  
  36. }
  37.  
  38. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  39. return
  40. }
  41.  
  42. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  43. {
  44. if selectindex[section] .isEqual("") {
  45.  
  46. return DataArr.count
  47.  
  48. }else{
  49.  
  50. return
  51. }
  52.  
  53. }
  54. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  55.  
  56. {
  57. let indenfer = "cell"
  58. let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: indenfer)
  59.  
  60. cell.textLabel?.text = DataArr[indexPath.row] as? String
  61.  
  62. return cell
  63. }
  64.  
  65. func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  66. return
  67. }
  68. func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  69.  
  70. let str:String = "-----------" + "\(section)" + "-------------"
  71.  
  72. return str
  73.  
  74. }
  75.  
  76. func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  77.  
  78. let headerview:UIView = UIView()
  79. headerview.frame = CGRectMake(, , ALLwidth,)
  80. headerview.backgroundColor = UIColor.grayColor()
  81. let lable:UILabel = UILabel()
  82. lable.frame = headerview.bounds
  83. lable.text = "-----------" + "第\(section)组" + "-------------"
  84. lable.textColor = UIColor.redColor()
  85. headerview.addSubview(lable)
  86.  
  87. let tap = UITapGestureRecognizer.init(target: self, action:#selector(ViewController.clicktap(_:)))
  88. headerview.tag = section
  89. headerview.addGestureRecognizer(tap)
  90.  
  91. return headerview
  92. }
  93.  
  94. func clicktap(tap:UITapGestureRecognizer){
  95.  
  96. print("=====\(tap.view?.tag)")
  97. let index:Int = tap.view!.tag
  98.  
  99. if selectindex[index] .isEqual(""){
  100.  
  101. selectindex.replaceObjectAtIndex(index, withObject: "")
  102.  
  103. }else{
  104.  
  105. selectindex.replaceObjectAtIndex(index, withObject: "")
  106. }
  107.  
  108. tableview .reloadData()
  109.  
  110. }
  111.  
  112. override func didReceiveMemoryWarning() {
  113. super.didReceiveMemoryWarning()
  114. // Dispose of any resources that can be recreated.
  115. }
  116.  
  117. }

tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift的更多相关文章

  1. iOS tableViewCell 在cell赋值、网络加载照片位置偏移大小错乱,做一个类似qq列表的tableview 更新3

    更新3: 问题 加载慢!(一时间给的处理负载过大,要分散)在下载图片,判断状态后 对每个cell对图片灰置图片处理保存,影响了主线程的操作 :上拉加载时,无法上下滑动tableview 无法点击cel ...

  2. android开发之ExpandableListView的使用,实现类似QQ好友列表

    由于工作需要,今天简单研究了一下ExpandableListView,做了一个类似QQ列表的Demo,和大家分享一下. 效果图如下: 先来看看主布局文件: <RelativeLayout xml ...

  3. 实现类似QQ的折叠效果

    //  主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. //  ViewController.m//  实现类似QQ的折叠效果/ ...

  4. Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)

    分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...

  5. 使用plupload做一个类似qq邮箱附件上传的效果

    公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...

  6. [C# 网络编程系列]专题九:实现类似QQ的即时通信程序

    转自:http://www.cnblogs.com/zhili/archive/2012/09/23/2666987.html 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例 ...

  7. .net winForm 实现类似qq 弹出新闻

    .net winForm 实现类似qq 弹出新闻   一.背景: echong 之前一直用 公司大牛c语言写的一个弹出托管,前几天写东西的时候发现com调用不是那么好使.而手头上写的这个东西又是.ne ...

  8. jQuery实现的3个基础案例(仿QQ列表分组,二级联动下拉框,模拟员工信息管理系统)

    1.仿QQ列表分组 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&quo ...

  9. 详解C# 网络编程系列:实现类似QQ的即时通信程序

    https://www.jb51.net/article/101289.htm 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net ...

随机推荐

  1. Servlet方法之service()

    1.service一定要用吗?作用是什么?是不是在程序开始运行时,自动装载执行的系统方法(类似于main)? Service是类GenericServlet中最重要的方法,每次客户向服务器发出请求时, ...

  2. C#之抽象类、虚方法、重写、接口、密封类

    前言    学了这么长时间的C#,我想说对于这个东东还是不是特别了解它,以至于让我频频郁闷.每次敲代码的时候都没有一种随心所欲的感觉.所以不得不在网上搜集一些资料,look 了 look~ 内容   ...

  3. 图解Linux安装jdk

    测试是否安装成功: 查看Java的版本命令:java -version Windows:查看java版本的方法是:运行--->cmd,输入java –version.注意: linux:终端中输 ...

  4. Nacos深入浅出(十)

    基本上到第9篇,整个请求的一套就结束了,感觉这里跳跳绕绕很多东西,下面我们来做个总结:从Nacos配置平台修改,到Client请求更新,事件触发去取值返回给客户端,整个过程感觉只分析到了4.5层的深度 ...

  5. kali源

      apt源: #中科大 deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib deb-src http://m ...

  6. CSS之flex兼容

    我觉得写的很好的文章,但是我又没有时间去整理的. https://blog.csdn.net/u010130282/article/details/52627661 百分比 是在宽度自适应的时候要用 ...

  7. JS键盘事件之键控Div

    自上次做的鼠标拖动Div之后,看到fgm.cc的例子,发现用键盘操控Div貌似也是十分有趣,这些DOM操作随着jquery的没落,虽然渐渐少用了,不过有些DOM操作还是必不可少的.现在是虽然数据为王( ...

  8. rsync简单总结

    rsync是一个远程数据同步工具,算法是同步文件差异部分,因此针对非第一次同步传输速度快 (首次备份,没有复制优势)rsync作者:Andrew Tridgell 和 Paul Mackerras r ...

  9. An internal error occurred during: "Add Deployment". Container with path org.eclipse.jdt.launching.

    导入非本机项目出现这种错误,原因就是JDK版本不一致. 具体解决步骤如下: 右键项目名→Properties→JavaBuild Path→Libraries→选中JRE SystemLibrary[ ...

  10. sql For update

    for update 的作用和目的:select for update 是为了在查询时,对这条数据进行加锁,避免其他用户以该表进行插入,修改或删除等操作,造成表的不一致性. 几个类似的场景: sele ...