IOS UITableView Section下拉列表实现

转载请注明我博客的地址: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值