可展开的UITableView (附源码)
本文转载至 http://www.apkbus.com/forum.php?mod=viewthread&tid=137207&extra=page%3D1
由于工作需要,写了一个UITableView的子类,简单的实现了每个cell的展开和收缩的动画效果以及展开和收缩后的cell样式变化。这个效果也许你现在用不到,但是它在iOS上的效果确实很不错,也许以后你就会用到。分享给大家。给大家一个实际的效果:
<ignore_js_op>
ExtensibleTableView.h
- //
- // ExtensibleTableView.h
- // Wow
- //
- // Created by Boris Sun on 12-6-20.
- // Copyright (c) 2012年 adsit. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @protocol ExtensibleTableViewDelegate <NSObject>
- @required
- //返回展开之后的cell
- - (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath;
- //返回展开之后的cell的高度
- - (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
- @end
- @interface ExtensibleTableView : UITableView
- {
- //当前被展开的索引
- NSIndexPath *currentIndexPath;
- id<ExtensibleTableViewDelegate> delegate_extend;
- }
- @property(nonatomic,retain)id delegate_extend;
- @property(nonatomic,retain)NSIndexPath *currentIndexPath;
- //将indexPath对应的row展开
- - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop;
- //将展开的cell收起
- - (void)shrinkCellWithAnimated:(BOOL)animated;
- //查看传来的索引和当前被选中索引是否相同
- - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath;
- @end
复制代码
ExtensibleTableView.m
- //
- // ExtensibleTableView.m
- // Wow
- //
- // Created by Boris Sun on 12-6-20.
- // Copyright (c) 2012年 adsit. All rights reserved.
- //
- #import "ExtensibleTableView.h"
- @implementation ExtensibleTableView
- @synthesize delegate_extend;
- @synthesize currentIndexPath;
- - (id)init
- {
- currentIndexPath = nil;
- return [super init];
- }
- //重写设置代理的方法,使为UITableView设置代理时,将子类的delegate_extend同样设置
- - (void)setDelegate:(id<UITableViewDelegate>)delegate
- {
- self.delegate_extend = delegate;
- [super setDelegate:delegate];
- }
- /*
- 将indexPath对应的row展开
- params:
- animated:是否要动画效果
- goToTop:展开后是否让到被展开的cell滚动到顶部
- */
- - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop
- {
- NSLog(@"debug 2");
- //被取消选中的行的索引
- NSIndexPath *unselectedIndex = [NSIndexPath indexPathForRow:[currentIndexPath row] inSection:[currentIndexPath section]];
- //要刷新的index的集合
- NSMutableArray *array1 = [[NSMutableArray alloc]init];
- //若当前index不为空
- if(currentIndexPath)
- {
- //被取消选中的行的索引
- [array1 addObject:unselectedIndex];
- }
- //若当前选中的行和入参的选中行不相同,说明用户点击的不是已经展开的cell
- if(![self isEqualToSelectedIndexPath:indexPath])
- {
- //被选中的行的索引
- [array1 addObject:indexPath];
- }
- //将当前被选中的索引重新赋值
- currentIndexPath = indexPath;
- if(animated)
- {
- [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade];
- }
- else
- {
- [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationNone];
- }
- if(goToTop)
- {
- //tableview滚动到新选中的行的高度
- [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
- }
- }
- //将展开的cell收起
- - (void)shrinkCellWithAnimated:(BOOL)animated
- {
- //要刷新的index的集合
- NSMutableArray *array1 = [[NSMutableArray alloc]init];
- if(currentIndexPath)
- {
- //当前展开的cell的索引
- [array1 addObject:currentIndexPath];
- //将当前展开的cell的索引设为空
- currentIndexPath = nil;
- [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade];
- }
- }
- //查看传来的索引和当前被选中索引是否相同
- - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath
- {
- if(currentIndexPath)
- {
- return ([currentIndexPath row] == [indexPath row]) && ([currentIndexPath section] == [indexPath section]);
- }
- return NO;
- }
- /*
- 重写了这个方法,却无效,因为这个方法总在didSelect之前调用,很奇怪。因为无法重写该方法,所以ExtensibleTableView不算完善,因为还有额外的代码需要在heightForRowAtIndexPath和cellForRowAtIndexPath中。哪个找到完善的方法后希望可以与qq82934162联系或者在http://borissun.iteye.com来留言
- */
- //- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
- //{
- // if([currentIndexPath row] == [indexPath row])
- // {
- // return [self.delegate_extend tableView:self extendedCellForRowAtIndexPath:indexPath];
- // }
- // return [super cellForRowAtIndexPath:indexPath];
- //}
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if([currentIndexPath row] == [indexPath row])
- {
- return [self.delegate_extend tableView:self extendedHeightForRowAtIndexPath:indexPath];
- }
- return [super rowHeight];
- }
- @end
复制代码
将这2个文件放到proj之后,要设置delegate_extend并且实现
//返回展开之后的cell
- (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath;
//返回展开之后的cell的高度
- (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
2个方法。
还有一点不合理的地方,我试着去解决,但是最终未果=。=!
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //若当前行被选中,则返回展开的cell
- if([tableView_ isEqualToSelectedIndexPath:indexPath])
- {
- return [self tableView:tableView extendedCellForRowAtIndexPath:indexPath];
- }
- ...
- }
复制代码
这里要先判断当前行是否被选中,若被选中则调用extendedCellForRowAtIndexPath方法。因为我试着重写UITableView的- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。试图在这个方法里做上边的事情,可是这个方法总是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法之前被调用,因此没有达到预期的目标。
<ignore_js_op>
ExtensibleTableViewDemo.zip (30.88 KB, 下载次数: 53)
可展开的UITableView (附源码)的更多相关文章
- (原创)通用查询实现方案(可用于DDD)[附源码] -- 设计思路
[声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html). [系列文章] 通用查询实现方案(可用于DDD)[附源码] -- ...
- 通用查询实现方案(可用于DDD)[附源码] -- 设计思路
原文:通用查询实现方案(可用于DDD)[附源码] -- 设计思路 [声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html). ...
- PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...
- 在网站开发中很有用的8个 jQuery 效果【附源码】
jQuery 作为最优秀 JavaScript 库之一,改变了很多人编写 JavaScript 的方式.它简化了 HTML 文档遍历,事件处理,动画和 Ajax 交互,而且有成千上万的成熟 jQuer ...
- Web 开发中很实用的10个效果【附源码下载】
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(二:附源码)
前言:上篇介绍了下 MVC5 的核心原理,整篇文章比较偏理论,所以相对比较枯燥.今天就来根据上篇的理论一步一步进行实践,通过自己写的一个简易MVC框架逐步理解,相信通过这一篇的实践,你会对MVC有一个 ...
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码)
前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个基于bootstrap的其他组件.和上篇不同的是,这篇的有几个组件需要某些js文件的支持. 本文原创地址:http://www.cnblog ...
- 轻量级通信引擎StriveEngine —— C/S通信demo(2) —— 使用二进制协议 (附源码)
在网络上,交互的双方基于TCP或UDP进行通信,通信协议的格式通常分为两类:文本消息.二进制消息. 文本协议相对简单,通常使用一个特殊的标记符作为一个消息的结束. 二进制协议,通常是由消息头(Head ...
- jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
上一篇记录了BaiduTemplate模板引擎使用示例附源码,在此基础上对使用方法进行了封装 自定义插件jajaxrefresh.js 代码如下: //闭包限定命名空间 (function ($) { ...
随机推荐
- LeetCode OJ-- Reverse Words in a String
https://oj.leetcode.com/problems/reverse-words-in-a-string/ 给一个字符串 abc dd m,返回 m dd abc. 注意:输入中可能有 ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom-强连通分量(Tarjan)
本来分好组之后,就确定好了每个人要学什么,我去学数据结构啊. 因为前一段时间遇到一道题是用Lca写的,不会,就去学. 然后发现Lca分为在线算法和离线算法,在线算法有含RMQ的ST算法,前面的博客也写 ...
- Codeforces Gym100814 I.Salem-异或 (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)
这个题就是二进制,找两个数相应的二进制相对应的位置上数不同的最多的个数.异或写就可以. 一开始还想麻烦了,找出来最大的偶数和最大的奇数,最小的偶数和最小的奇数,但是这样想考虑的不全.因为范围比较小,直 ...
- MySQL 如何优化cpu消耗
目录 谁在消耗cpu? 祸首是谁? 用户 IO等待 产生影响 如何减少CPU消耗? 减少等待 减少计算 升级cpu 谁在消耗cpu? 用户+系统+IO等待+软硬中断+空闲 祸首是谁? 用户 用户空间C ...
- Algorithm | hash
A basic requirement is that the function should provide a uniform distribution of hash values. A non ...
- Unix/Linux提权漏洞快速检测工具unix-privesc-check
Unix/Linux提权漏洞快速检测工具unix-privesc-check unix-privesc-check是Kali Linux自带的一款提权漏洞检测工具.它是一个Shell文件,可以检测 ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
- Jackson是线程安全的吗
网上说是线程安全的,内部代码用了ThreadLocal.Synchronized这些线程安全类和关键字,可以放心的用. 避免每次使用都new一个,全局配置一个ObjectManager的对象将大大减少 ...
- PHP处理Android的POST数据
今天用PHP开发Android网络数据接口的时候,发现Thinkphp的I函数(php的$_POST)并不能获取到androidpost过来的数据 Android代码如下: Map<String ...
- static再次深入理解
在java中,栈中存放的是用来保存方法运行时状态的栈帧,存储了局部变量表,操作数栈等,而方法区存放的是已加载的类的基本信息.常量.静态变量等.