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)returnNewLostHt39

30NewLostHt;//不显示没有展开的

 //.................原有的代码
}
 
 
第三步:
     实现自定义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
原创粉丝点击