tableview中cell折叠的功能
创建这样的文件
#import <UIKit/UIKit.h>
/**分组*/
@interface YYMGroup : NSObject
//联系人数据
@property(nonatomic,strong)NSMutableArray *items;
//大小(分组中有多少项)
@property (nonatomic, readonly) NSUInteger size;
//是否折叠
@property (nonatomic, assign, getter=isFolded) BOOL folded;
//初始化方法
- (instancetype) initWithItem:(NSMutableArray *)item;
@end
#import "YYMGroup.h"
@implementation YYMGroup
//初始化方法
- (instancetype) initWithItem:(NSMutableArray *)item{
if (self = [super init]) {
self.folded=YES;
_items = item;
}
return self;
}
//每个组内有多少联系人
- (NSUInteger) size {
return _items.count;
}
@end
#import "ViewController.h"
#import "YYMGroup.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
UITableView *myTableView;//定义tabview
NSMutableArray *dataArray;//数据源数组
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建tableview;
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT) style:UITableViewStylePlain];
[self.view addSubview:myTableView];
myTableView.dataSource = self;
myTableView.delegate = self;
//绑定数据源
[self loadDataModel];
}
- (void) loadDataModel {
if (!dataArray) {
dataArray = [NSMutableArray array];
}
// Todo: 加载数据模型(后台请求的数据)
NSArray *groupNames = @[@[@"张无忌",@"狄云",@"狄青",@"李慕白",@"张飞"],@[@"李宗宪",@"张学良",@"嬴政",@"大禹"],@[@"陆小凤",@"陆依萍",@"李云龙",@"李自成"],@[@"魏征",@"白展堂",@"花无缺",@"云彦民"]];
//这是一个分组的模型类
for (NSMutableArray *name in groupNames) {
YYMGroup *group1 = [[YYMGroup alloc] initWithItem:name];
[dataArray addObject:group1];
}
}
#pragma mark UITableViewDataSource回调方法
//这是tabview创建多少组的回调
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return dataArray.count;
}
//这是每个组有多少联系人的回调
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
YYMGroup *group = dataArray[section];
return group.isFolded? 0: group.size;
}
//将tabview的cell与数据模型绑定起来
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL"];
}
//将模型里的数据赋值给cell
YYMGroup *group = dataArray[indexPath.section];
NSArray *arr=group.items;
cell.textLabel.text = arr[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark UITableViewDelegate回调方法
//对hearderView进行编辑
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//首先创建一个大的view,nameview
UIView *nameView=[[UIView alloc]init];
//将分组的名字nameLabel添加到nameview上
UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, self.view.frame.size.width, 40)];
[nameView addSubview:nameLabel];
nameView.layer.borderWidth=0.2;
nameView.layer.borderColor=[UIColor grayColor].CGColor;
NSArray *nameArray=@[@" 老朋友",@" 同事",@" 网友",@" 游戏朋友"];
nameLabel.text=nameArray[section];
//添加一个button用于响应点击事件(展开还是收起)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, self.view.frame.size.width, 40);
[nameView addSubview:button];
button.tag = 200 + section;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//将显示展开还是收起的状态通过三角符号显示出来
UIImageView *fuhao=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
fuhao.tag=section;
[nameView addSubview:fuhao];
//根据模型里面的展开还是收起变换图片
YYMGroup *group = dataArray[section];
if (group.isFolded==YES) {
fuhao.image=[UIImage imageNamed:@"右边"];
}else{
fuhao.image=[UIImage imageNamed:@"下边"];
}
//返回nameView
return nameView;
}
//设置headerView高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
//设置cell的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
//button的响应点击事件
- (void) buttonClicked:(UIButton *) sender {
//改变模型数据里面的展开收起状态
YYMGroup *group2 = dataArray[sender.tag - 200];
group2.folded = !group2.isFolded;
[myTableView reloadData];
}
@end
tableview中cell折叠的功能相关推荐
- 设定所有tableView中cell的分隔线颜色
上面只有针对xib或者storyboard中生成的tableview有效,如果想手码也有效,需在initwithframe中添加同样的方法 转载于:https://www.cnblogs.com/so ...
- 动态切换tableView中的cell的种类
动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...
- UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件...
一 .问题:你想用流畅直观的动画来移动和拖拽TableView中的cell和section 方案: 用moveSection:toSection:方法把一个Section移动到新位置. 用moveR ...
- IOS TableView的Cell高度自适应,UILabel自动换行适应
需求: 1.表格里的UILable要求自动换行 2.创建的tableViewCell的高度会自动适应内容的高度 一.用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现: 1. ...
- Tableview中Dynamic Prototypes动态表的使用
Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...
- Unity实用小工具或脚本——可折叠伸缩的多级(至少三级)内容列表(类似于Unity的Hierarchy视图中的折叠效果)
目录 一.前言 二.实现 2.1.创建ScrollView 2.2.制作层级预设体BaseLevelPartObj 2.3.设置该预设体的初始化处理方法 2.4.读取Hierarchy的内容并创建UI ...
- Swift.自定制相册,实现首个cell是拍照功能
前言: 过去有个项目有了这个需求.当时选择从git上找了个框架直接来用,但是其中很多功能是我并不需要的,以及想要了解一下这一块的具体实现方式,就选择自己参考其重写了一下,主要运用的知识就是photos ...
- [Tool] 插入折叠区域功能
之前写了一个 仿博客园网页端推荐的插入代码插件, 后来在总结一些技术文档时,总是想把一些属性或者方法,参数等,都用表格的形式清晰的列举出来,但是插入的表格太大的话,上下跨度就显得特别大,来回上下滚动的 ...
- 使java方法全部折叠_Intellij IDEA中一次性折叠所有Java代码的快捷键设置
Intellij IDEA中一次性折叠所有Java代码的快捷键设置 问题:在Java文件中,想把所有的Java方法代码都一次性给折叠起来,用哪个点开哪个. 问题来源:在新建model bean的时候, ...
- android读取工程目录下的文件,Android编程实现读取工程中的txt文件功能
本文实例讲述了Android编程实现读取工程中的txt文件功能.分享给大家供大家参考,具体如下: 1. 众所周知,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹 ...
最新文章
- WPF查找子控件和父控件方法
- netfilter的笔记3--那些内置的表
- python判断变量不为空_Python简洁方法
- 2019 Python100道 面试 题,你会几道?
- SAP UI5 Component-preload.js
- combox 增加请选择_娱乐测试:选择四种花束中的一种,测试你对婚姻的看法
- Dev Treelist使用经验(2)
- 解决easyExcel和poi版本冲突问题
- 海量ICLR论文点评公开,用这几个工具可以读得更轻松
- 为什么很多人愿意去下载社交APP?
- 配置微信公众号之公众号绑定小程序
- 【英语】December
- 简单明了的告诉你什么是CPUID
- centos 7 JDK 环境部署
- 苹果cms V8/V10定时任务百度主动URL推送设置教程 配合宝塔定时计划任务
- 防止前端重复提交表单
- 【信息】三方协议签订相关问题及解答
- python爬取微博用户的微博内容和图片
- Linux中文件的压缩和解压缩
- 计算机 交换机和IP/Mac地址
热门文章
- Recipe 1.5. Trimming Space from the Ends of a String(Python Cookbook)
- android 推送图标大小,设计方法论:一种统一图标大小的方法
- {转载}——jvm verbose 参数详解
- Flutter 项目实战 截图分享到微信|QQ|微博 十二
- python模型保存与恢复_tensorflow1.0学习之模型的保存与恢复(Saver)_python
- socket套接字编程 | 粘包现象 | 报头 | struck模块
- 【无标题】对于_, predicted = torch.max(outputs.data, 1)的理解
- 人可以活很多次,但是七年就是一辈子
- android开发,动态图标,Android动态加载很难?带你快速实现App图标热更新
- 文件及文件夹管理规范