tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift
之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个cell同时展开,只能一个一个的展开。
我就尝试用用数组记录展开的标记的方法,功能实现了,
直接上代码:
//
// ViewController.m
// 折叠tableview-Test
//
// Created by abc on 16/7/26.
// Copyright © 2016年 LiuWenqiang. All rights reserved.
// #import "ViewController.h" #define ALLWIDTH [UIScreen mainScreen].bounds.size.width
#define ALLHEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView*tableview;
NSArray *dataArr;
NSMutableArray *isOpenArr; }
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; tableview = [[UITableView alloc]initWithFrame:CGRectMake(, , ALLWIDTH, ALLHEIGHT-)];
tableview.delegate =self;
tableview.dataSource = self;
tableview.tableFooterView = [[UIView alloc]init];
[self.view addSubview:tableview]; dataArr = [NSArray array];
dataArr = @[@"呵呵",@"哈哈",@"嗯嗯",@"呃呃",@"很好"]; isOpenArr = [NSMutableArray array]; for (int i =; i< dataArr.count; i++) { [isOpenArr addObject:@""];
} }
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([isOpenArr[section] isEqualToString:@""])
{
return dataArr.count;
}else{
return ;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellstr = @"cell";
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr];
if (!cell) {
cell = [tableview dequeueReusableCellWithIdentifier:cellstr forIndexPath:indexPath];
} cell.textLabel.text = dataArr[indexPath.row]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return ;
} -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view =[[UIView alloc]init];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, ,ALLWIDTH, )];
lable.text = [NSString stringWithFormat:@"------------第%ld组---------",(long)section];
lable.textColor = [UIColor redColor];
[view addSubview:lable]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topgesture:)];
[view addGestureRecognizer:tap]; view.tag = section;
return view; }
//在组头上添加点击手势
-(void)topgesture:(UITapGestureRecognizer*)tap
{
NSInteger index = tap.view.tag; if ([isOpenArr[index] isEqualToString:@""]) { [isOpenArr replaceObjectAtIndex:index withObject:@""]; }else{
[isOpenArr replaceObjectAtIndex:index withObject:@""]; }
[tableview reloadData]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableview deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"------================--%ld-----",indexPath.row);
} @end
自己最近几天正在学习swifit,于是就试着用swift 写了一遍,
//
// ViewController.swift
// 测试--Swift--Test
//
// Created by abc on 16/7/26.
// Copyright © 2016年 LiuWenqiang. All rights reserved.
// import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let ALLwidth = UIScreen.mainScreen().bounds.width
let ALLheight = UIScreen.mainScreen().bounds.height var tableview:UITableView = UITableView()
var DataArr: NSMutableArray = NSMutableArray() var selectindex:NSMutableArray = NSMutableArray() override func viewDidLoad() {
super.viewDidLoad() tableview.frame = CGRectMake(, , ALLwidth, ALLheight)
tableview.delegate = self
tableview.dataSource = self
self.view.addSubview(tableview) DataArr = ["第一行","第二行","第三行","第四行"] for _ in DataArr { selectindex.addObject("")
} } func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return
} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if selectindex[section] .isEqual("") { return DataArr.count }else{ return
} }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let indenfer = "cell"
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: indenfer) cell.textLabel?.text = DataArr[indexPath.row] as? String return cell
} func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { let str:String = "-----------" + "\(section)" + "-------------" return str } func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerview:UIView = UIView()
headerview.frame = CGRectMake(, , ALLwidth,)
headerview.backgroundColor = UIColor.grayColor()
let lable:UILabel = UILabel()
lable.frame = headerview.bounds
lable.text = "-----------" + "第\(section)组" + "-------------"
lable.textColor = UIColor.redColor()
headerview.addSubview(lable) let tap = UITapGestureRecognizer.init(target: self, action:#selector(ViewController.clicktap(_:)))
headerview.tag = section
headerview.addGestureRecognizer(tap) return headerview
} func clicktap(tap:UITapGestureRecognizer){ print("=====\(tap.view?.tag)")
let index:Int = tap.view!.tag if selectindex[index] .isEqual(""){ selectindex.replaceObjectAtIndex(index, withObject: "") }else{ selectindex.replaceObjectAtIndex(index, withObject: "")
} tableview .reloadData() } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift的更多相关文章
- iOS tableViewCell 在cell赋值、网络加载照片位置偏移大小错乱,做一个类似qq列表的tableview 更新3
更新3: 问题 加载慢!(一时间给的处理负载过大,要分散)在下载图片,判断状态后 对每个cell对图片灰置图片处理保存,影响了主线程的操作 :上拉加载时,无法上下滑动tableview 无法点击cel ...
- android开发之ExpandableListView的使用,实现类似QQ好友列表
由于工作需要,今天简单研究了一下ExpandableListView,做了一个类似QQ列表的Demo,和大家分享一下. 效果图如下: 先来看看主布局文件: <RelativeLayout xml ...
- 实现类似QQ的折叠效果
// 主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. // ViewController.m// 实现类似QQ的折叠效果/ ...
- Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)
分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...
- 使用plupload做一个类似qq邮箱附件上传的效果
公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...
- [C# 网络编程系列]专题九:实现类似QQ的即时通信程序
转自:http://www.cnblogs.com/zhili/archive/2012/09/23/2666987.html 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例 ...
- .net winForm 实现类似qq 弹出新闻
.net winForm 实现类似qq 弹出新闻 一.背景: echong 之前一直用 公司大牛c语言写的一个弹出托管,前几天写东西的时候发现com调用不是那么好使.而手头上写的这个东西又是.ne ...
- jQuery实现的3个基础案例(仿QQ列表分组,二级联动下拉框,模拟员工信息管理系统)
1.仿QQ列表分组 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&quo ...
- 详解C# 网络编程系列:实现类似QQ的即时通信程序
https://www.jb51.net/article/101289.htm 引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net ...
随机推荐
- Ocelot - .Net Core开源网关
Ocelot - .Net Core开源网关 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10857688.html 源码地 ...
- 洛谷P3803 【模板】多项式乘法(FFT)
P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...
- [Xcode 实际操作]四、常用控件-(12)环形进度条控件的使用
目录:[Swift]Xcode实际操作 本文将演示环形进度条控件的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vi ...
- [Xcode 实际操作]四、常用控件-(7)UIStepper控件的使用
目录:[Swift]Xcode实际操作 本文将演示步进控件的基本用法.步进控件常用于小范围数值的调整. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import ...
- Fiddler 基础教程详解
Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...
- HDFS(Hadoop Distributed File System )hadoop分布式文件系统。
HDFS(Hadoop Distributed File System )hadoop分布式文件系统.HDFS有如下特点:保存多个副本,且提供容错机制,副本丢失或宕机自动恢复.默认存3份.运行在廉价的 ...
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
前言: 苹果在IOS 6系统之后开始支持BLE 4.0,iPhone4s,iPod 5,iPad 3等之后的机型开始内嵌BLE4.0硬件,因此在开发前请先确认你的开发环境符合上述要求,并且苹果在BLE ...
- oracle odbc连接sqlserver 无法查询字段
最近因项目需要,需要在oracle数据库里面通过DBLINK方式链接SQLserver数据库(oracle 11G,Sqlserver2016,具体实现参考链接 http://blog.sina.co ...
- CentOS编译安装GCC 4.9.2成功
在Linux上编译安装gcc是个寻烦恼的活,对于像我这样习惯于在Windows上面使用二进制安装包的人来说,自已编译安装gcc是个相当大的挑战,今天直接挑战最新版的gcc,是4.9.2版本的,做之前查 ...
- P4135 作诗
传送门 分块 设sum[ i ] [ j ] 存从左边到第 i 块时,数字 j 的出现次数 f [ i ] [ j ] 存从第 i 块,到第 j 块的一整段的答案 那么最后答案就是一段区间中几块整段的 ...