可展开的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 ($) { ...
随机推荐
- hdu 4523(大整数)
威威猫系列故事——过生日 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- Topcoder SRM 145 DIV 1
Bonuses 模拟 题意:给你一个序列,按照百分比排序,再将百分比取整,再把剩余百分比加到最大的那几个. 题解:按照题意模拟就好.
- [UR #3] 核聚变反应强度
次大公约数就是gcd再除以其最小质因子(如果有的话).可以发现要求的sgcd 的前身gcd都是a1的约数,所以把a1质因数分解直接做就行了. #include<bits/stdc++.h> ...
- MD5 algorithm in Objective C
How to calculate the MD5 in objective C ? md5 is available on the iPhone and can be added as an exte ...
- xamarin.ios 半圆角按钮Readerer
xamarin.from上可以使用本身的button实现圆角带图标的按钮,但是没有半圆角的按钮实现,需要自己使用Renderer重新写过来重写一个button. 下面是一个重写的带边框的方式,代码如下 ...
- dedecms 调用栏目或文章所属上下级关系
效果如下: 代码如下: <div class="place"> <strong>当前位置:</strong> {dede:field name= ...
- Can''t find the channel handler for deviceType 工行 个人网银 错误
背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话 Can''t find the channel handler for devic ...
- EasyMvc入门教程-基本控件说明(13)选项卡导航
选项卡Tab导航主要用于企业页面显示不同子类或者子页面的信息内容. 先来一个基本的使用例子:代码如下: @{ var data = new List<TabItem>() { new Ta ...
- 【GLSL教程】(七)逐像素的光照 【转】
http://blog.csdn.net/racehorse/article/details/6662540 逐像素的方向光(Directional Light per Pixel) 这一节将把前面的 ...
- 时间迭代和BigDecimal操作
常规小操作的代码: import java.math.BigDecimal; import java.sql.Timestamp; import java.text.SimpleDateFormat; ...