可折叠tableView - 仿照通讯录

我的xcode 是    IOS9.0    Xcode.7.0

版本符合,可复制黏贴直接用

#import "ViewController.h"

1.定义成员变量 遵循代理

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

NSMutableArray *_switchArray;

NSMutableArray *_contentArray;

UITableView *_tableView;

}

@end

@implementation ViewController

2.  viewDidLoad  里

- (void)viewDidLoad {

[super viewDidLoad];

[self getData];

[self addUITableView];

}

3. addUITableView 里

- (void)addUITableView{

_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; //设置为朴素样式

_tableView.delegate = self;

_tableView.dataSource = self;

[self.view addSubview:_tableView];

}

//UITableViewDataSource 方法

/*

必须实现的方法

 方法一: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 方法二:  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

可选实现的方法

方法一: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

方法二: - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

方法三:- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

方法四:- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

方法五:- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

方法六:- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

方法七:- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

方法八:- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

方法九:- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

*/

4 numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (![[_switchArray objectAtIndex:section] boolValue]) {

return 0;// 闭合状态

}else{

return [[_contentArray objectAtIndex:section] count];// 每一组的行数

}

}

5.cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *cellID = @"CELL";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}

NSArray *array = _contentArray[indexPath.section];// 取出对应组

NSString *title = array[indexPath.row];//取出对应组

cell.textLabel.text = title;

return cell;

}

6.titleForHeaderInSection 头标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"fwz";

}

7. viewForHeaderInSection :头视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIButton *button = [[UIButton alloc]init];

[button setTitle:[NSString stringWithFormat:@"%c",(char)(‘A‘ + section)] forState:UIControlStateNormal];

NSLog(@"1%@",button.titleLabel.text);

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

button.tag = 100 + section;

[button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];

return button;

}

8.tap button的点击事件

-(void)tap:(UIButton *)sender{

//1.取出BOOL值

BOOL isOpen = [[_switchArray objectAtIndex:sender.tag - 100] boolValue];

// 2.把Bool 取反

[_switchArray replaceObjectAtIndex:sender.tag - 100 withObject:[NSNumber numberWithBool:!isOpen]];

// 3.tableview做相应的操作(刷新某一组)

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag -100] withRowAnimation:UITableViewRowAnimationAutomatic];

}

9.getData

- (void)getData{

//记录我们组的开关装态

_switchArray = [NSMutableArray array];

_contentArray = [NSMutableArray array];

for (int i = ‘A‘; i <= ‘Z‘; i ++) {

NSMutableArray *arrayNuMm = [NSMutableArray array];

for (int j = 0; j <  8; j ++) {

[arrayNuMm addObject:[NSString stringWithFormat:@"%c————%d",i,j]];//每一组的行数

}

[_contentArray addObject:arrayNuMm];// 26组

[_switchArray addObject:[NSNumber numberWithBool:NO]];// 默认为NO

}

}

@end

时间: 2024-08-05 13:35:28

可折叠tableView - 仿照通讯录的相关文章

iOS中文版资源库,非常全

目录 入门 库和框架 音频 动画 Apple TV 桥接 缓存 Core Data 图表 数据库 硬件 动作 蓝牙 位置 iBeacon HUD 事件总线( EventBus ) 文件 JSON 布局 日志 地图 媒体 图片 视频 PDF 消息 网络 推送通知 Passbook 权限 文本 浏览 / 介绍 / 教程 URL Scheme UI Websocket 代码质量 分析 支付 产品化工具 实用工具 安全 安装项目 依赖 / 包管理 测试 测试驱动开发(TDD) / 行为驱动开发(BDD)

iOS中文版资源库

我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-ios 就是 vsouza 发起维护的 iOS 资源列表,内容包括:框架.组件.测试.Apple Store.SDK.XCode.网站.书籍等.Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的项目则会被标记为 ▲. Awesome 系列虽然挺全,但基本只对收录的资源做了极为简要的介绍,如果有更详细的中文介绍,对相应开发者的帮助会更大.这也是我们发起这个开源项目的初衷.

iOS-资源大全

本文由 伯乐在线 - ARIGATO 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:vsouza.欢迎加入翻译组. 这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Testing )和其他的开源项目,免费的和付费的服务.每个部分中的项目没有先后顺序,均是按照提交的先后顺序排列的. Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的

tableView加searchBar,以搜索通讯录为列

使用的是storyBoard 1拖一个UISearchDisplayController到tableView的headerView上,并声明成全局变量 @property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDispalyController; @property (weak, nonatomic) IBOutlet UISearchBar *searchBar; @property (nonatomic,

ios-私人通讯录 页面间的跳转和传值

这个demo 有多个页面 并涉及顺传和逆传 而且还有一个第三方库的导入 来实现自定义提示消息的特效 利用代理来实现页面间的传值 一个页面代表一个controller 这次  ViewController  反而一句代码都没写 // // HMContact.h // 私人通讯录 // // Created by YaguangZhu on 15/9/6. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <Fou

私人通讯录

一.私人通讯录 什么是Segue ● Storyboard上每?一根?用来界?面跳转的线,都是?一个UIStoryboardSegue对象(简称Segue) "[登录界面]" 1.掌握UIStoryboardSegue对象 (1)在storyboard中,可以通过连线完成多个界面间的跳转,每一连线我们称为segue (2)掌握storyboard中的segue连线有两种,一是自动跳转.二是手动跳转 自动跳转从按钮连线,不管账号和密码是不是正确,就直接跳到下一个界面,而手动跳转,是用控制

CoreData 实例 --- 通讯录

要求:将数据存放于CoreData数据库中,包括联系人的姓名和电话号码,将数据取出,显示在view上 详细代码如下 首先导入#import<CoreData/CoreData.h> ViewController.h 文件 static NSManagedObjectContext * _context; //上下文 @interface TXLViewController () { UITableView * _tableView; NSMutableArray * _array; TXLTa

iOS之iPhone手机通讯录和短信搜索界面的实现以及UISearchController和UISearchDisplayController的浅析

本来觉得这个模块也就是一个SearchBar就搞定了,但是现在的产品经理也是够了,一会儿一个想法,之前的搜索 都已经写完了,类似主流的电商,好像也没那么麻烦,但是改版了总得弄点什么吧.嘿,哥们,我现在要iphone手机 通讯录里面搜索的样式,你搞定哦......,要一毛一样哦.作为一个文化人,我只能在内心深处生 出表达出,苦逼的我们顶多发发牢骚,要改就改喽. 请看图先 这是他要的效果demo 下面是我写的demo 看到这效果,应该都能想到用UISearchController,但是这货是iOS8

iOS有关横向TableView的东西

之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionview了,点缀的使用一下横向的tableview反而更方便和灵活.当然此处大部分的情况可能是在父tableview的cell中使用个横向的tableview. 下面就简单的介绍一下,基于系统原生的UITableView封装横向tableview的要点. 我封装tableview的时候主要使用了一种比较流