可展开的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 ($) { ...
随机推荐
- android的动态代码
1,Android代码设置Shape,corners,Gradient (http://blog.csdn.net/houshunwei/article/details/17392409) int ...
- hdu 2654(欧拉函数)
Become A Hero Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- LeetCode OJ--Combination Sum **
https://oj.leetcode.com/problems/combination-sum/ 给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得 ...
- RecyclerView的Item和Item内的控件点击处理
需求场景:RecyclerView的Item需要点击,或者Item中的某个控件需要点击,或者两者同时需要点击处理. 一.adapter代码如下: package com.ldw.adapter; im ...
- App Store 审核指南(最新)
简介 App 正在改变世界,丰富人们的生活,并为像您一样的开发者提供前所未有的创新机会.因此,App Store 已成长为一个激动人心且充满活力的生态系统,正为数百万的开发者和超过十亿的用户提供服务. ...
- 解决dvajs使用BrowserHistory路由模式后仍然会出现hash(哈希)
在dvajs中,如果你在初始化dva对象的时候不作任何处理,那么你就会发现即使你是用了BrowserRouter来作为Router url中也是会出现#/.解决方法也很简单: 使用前先手动安装下 hi ...
- HDU 1020 Encoding【连续的计数器重置】
Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- codevs_1043 方格取数(棋盘DP)
1043 方格取数 2000年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description ...
- ThinkPHP示例:模板主题
ThinkPHP示例之模板主题,模板主题可以对相同的控制器输出进行不同的布局和样式调整.首先需要下载框架核心,然后把示例解压到Web根目录下面,并修改入口文件中的框架入口文件的位置.访问 http:/ ...
- mac复制文件命令
test1下有test01 test02两个文件 ,复制到test2下 则cp -r test1/ test2 权限不够,,则加sudo test2要事先存在, 如果复制test01到当前目录 cp ...