IOS UITableView Section下拉列表实现
来源:互联网 发布:武媚娘传奇知乎 编辑:程序博客网 时间:2024/05/20 02:52
转载请注明我博客的地址:http://blog.liuxia520.com/
今天要使用UITableView做个列表分类的,如果列表很多,而且Section名称还很长的话,使用Section就不是很直观了。为了节省时间,就在网上搜了下,发现大部分的demo看起来麻烦,对我原有的代码修改比较大,就想找个对我代码修改较少的UITableView下拉列表扩展实现,不过没找到,只好自己动手实现了。
第一步:
既然要使sectino能下拉,那么必须要自己定制section了,所以我定制的sectinoView包含以下部分
#import <UIKit/UIKit.h>@protocol HeaderDropSectionDelegate;@interface HeaderDropSection : UIButton{ UIImageView *rightImageView; BOOL _isOpen;}@property(nonatomic,assign)id<HeaderDropSectionDelegate> myDelegate;@property(nonatomic,assign)int sectionIndex;//该SectionView属于哪个section@property(nonatomic,assign)BOOL isOpen;//默认关闭@property(nonatomic,strong)UILabel *sectionTitleLable;//标题@end@protocol HeaderDropSectionDelegate <NSObject>//section点击委托-(void)HeaderDropSectionClick:(HeaderDropSection *)headerSectionView;@end
第二步:
设置UITableView Section 的代理
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSArray *array=[orderInfoDic allKeys]; HeaderDropSection *headerDropView=[[HeaderDropSection alloc]initWithFrame:CGRectMake(0, 0, mTableView.frame.size.width, 60)]; headerDropView.sectionTitleLable.text=[array objectAtIndex:section]; headerDropView.myDelegate=self; headerDropView.sectionIndex=section; if(selectHeaderSectionIndex==section) { [headerDropView setIsOpen:YES]; selectHeaderSectionIndex=section; } return headerDropView;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60;}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 1;}
//然后在返回section的代理中只需要增加一个判断,原来的逻辑不需要该
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//增加判断
if(section!=selectHeaderSectionIndex)returnNewLostHt3930NewLostHt;//不显示没有展开的
//.................原有的代码
}
第三步:
实现自定义section的代理
-(void)HeaderDropSectionClick:(HeaderDropSection *)headerSectionView{ [headerSectionView setIsOpen:YES]; //保存原来展开的section int oldIndex=selectHeaderSectionIndex; if(oldIndex!=-1)//闭合之前已经张开的section { selectHeaderSectionIndex=-1; [mTableView reloadSections:[NSIndexSet indexSetWithIndex:oldIndex] withRowAnimation:UITableViewRowAnimationFade]; } //如果当前section不是展开的,则设置展开,否则闭合 if(headerSectionView.sectionIndex!=oldIndex) selectHeaderSectionIndex=headerSectionView.sectionIndex; else selectHeaderSectionIndex=-1; //刷新需要展开的section if(selectHeaderSectionIndex!=-1) { [mTableView reloadSections:[NSIndexSet indexSetWithIndex:selectHeaderSectionIndex] withRowAnimation:UITableViewRowAnimationFade]; }}
对原来的代码增加以上部分就可以实现UITableView Section下拉功能了。
注:变量selectHeaderSectionIndex 用来保存当前正在展开的section,没有展开为-1,而且我这个目前设计为同时只能有一个section展开,如果需要多个seciton都能展开,可以使用数组保存需要展开的selectHeaderSectionIndex 。
由于是项目中得一个功能,所以源码就不上传了。有时间的补一个demo
0 0
- IOS UITableView Section下拉列表实现
- iOS UITableView中的下拉列表
- IOS之UITableView实现Section独立封装
- 【Demo】UITableView实现分组可折叠下拉列表
- iOS UITableView设置Section间距
- IOS UITableView下拉刷新
- iOS-----iOS UITableView 实现上拉及下拉刷新
- 【IOS功能实现】之:下拉列表
- iOS 简单实现下拉列表框
- 【IOS功能实现】之:下拉列表
- iOS开发之UITableView的下拉刷新实现
- iOS UITableView 实现上拉及下拉刷新
- iOS UITableView 实现上拉及下拉刷新
- iOS UITableView 实现上拉及下拉刷新
- IOS怎么实现一个UITableView的下拉刷新
- [iOS] 使用UIRefreshControl 实现 UITableView下拉刷新(Swift版本)
- ios uitableview 刷新某一个cell 或 section
- ios uitableview 刷新某一个cell 或 section
- Javadoc in Eclipse failing to recognize packages
- 实用性测试(Pragmatistic Testing)Seeing is not believing!Testing is believing!做个实用主义测试者
- ECLIPSE java.lang.OutOfMemoryError: PermGen space 错误
- Quartz.NET的管理工具
- Java做成Zip文件,Java实现压缩文件
- IOS UITableView Section下拉列表实现
- 程序员学习能力提升三要素
- ios 平铺图片
- 【Solr4.7.0学习笔记】系列之一:ik智能分词(需要导入lucene4.7jar包)
- 获取Ipnone当前的系统版本号
- C++一些宏定义
- 25条提高iOS App性能的技巧和诀窍
- 「C++」一个brainfuck 语言解释器
- matlab 的直方图均衡化