ios QQ下拉列表 UITableViewHeaderFooterView

QQ下拉列表,最近找了一下网上没有类似的例子,今天做了一个Demo

图片可以在评论里留言留下邮箱,


#import "ViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray * dataArray;

@property (nonatomic,weak)UITableView * tabelView;

@property (nonatomic,strong)NSMutableDictionary * imageDict;// 分组对应下拉箭头图片字典

@property (nonatomic,strong)NSMutableDictionary * stateDict;// 分组对应状态字典


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // 加载数据

    [self_loadData];

    // 加载视图

    [self_loadView];

}


#pragma mark -_loadData

- (void)_loadData{

    NSString * path = [[NSBundlemainBundle]pathForResource:@"friends.plist"ofType:nil];

    self.dataArray = [NSMutableArrayarrayWithContentsOfFile:path];

}


/**

 *  懒加载

 *

 *  @return <#return value description#>

 */

- (NSMutableDictionary *)imageDict{

   if (_imageDict ==nil) {

        _imageDict = [NSMutableDictionarydictionary];

    }

    return_imageDict;

}

- (NSMutableDictionary *)stateDict{

   if (_stateDict ==nil) {

        _stateDict = [NSMutableDictionarydictionary];

    }

    return_stateDict;

}

#pragma mark -_loadView

- (void)_loadView{

    UITableView * tableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStyleGrouped];

    tableView.delegate =self;

    tableView.dataSource =self;

    

    //设置分组头部和尾部的高度

    tableView.sectionFooterHeight =2;

    tableView.sectionHeaderHeight =40;

    tableView.rowHeight =60;

    

   self.tabelView = tableView;

    [self.viewaddSubview:tableView];

    

}


#pragma mark - UITableViewDataSource


#pragma mark - 分组头部高度// 不加第一组头显示不出来

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

   return 40;

}


#pragma mark - 返回分组数量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    

    return self.dataArray.count;

    

}


#pragma mark - 设置分组标题

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

//   id temp = self.dataArray[section];

//

//    return temp[@"group"];

//}


#pragma mark - 自定义头部视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

   static NSString * identy =@"headView";

// 类似cell的重利用机制

    UITableViewHeaderFooterView * hfVeiw = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:identy];

   if (!hfVeiw) {


        hfVeiw = [[UITableViewHeaderFooterViewalloc]initWithReuseIdentifier:identy];

        hfVeiw.contentView.backgroundColor = [UIColorgrayColor];

        

// 初始化button

        UIButton * button = [UIButtonbuttonWithType:UIButtonTypeCustom];

        button.frame =CGRectMake(0,0, self.view.frame.size.width,40);

        [hfVeiw.contentViewaddSubview:button];


        // 初始化imageView

        UIImageView * imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(5,12, 20, 20)];

        UIImage * image = [UIImageimageNamed:@"disclosure.png"];

        imageView.image = image;

        imageView.tag =1001;

        [buttonaddSubview:imageView];


        // button添加点击事件

        [button addTarget:selfaction:@selector(clickAction:)forControlEvents:UIControlEventTouchUpInside];


    }

    

   id temp = self.dataArray[section];

    

    // 拿出button重新设置标题

   UIButton * button = [hfVeiw.contentView.subviewsfirstObject];

    [button setTitle:temp[@"group"]forState:UIControlStateNormal];

   UIImageView * imageView = (UIImageView *)[buttonviewWithTag:1001];

    

    //通过tag值获取点击的是哪个组

    button.tag = section;//控件默认的tag都为0,获取未设置tag的控件,会崩,但是设置某控件tag0则不会

    

    // 将最新的ImageView加入到字典

    [self.imageDictsetObject:imageView forKey:@(section)];

    

   UIImageView * targetImageView = (UIImageView *)self.imageDict[@(section)];

    

// 为何出现此BUG,如何防止:1.下拉的时候出现重复利用,所以在重复利用后获取从池子取出的imageView,根据当前状态(打开、关闭)设置图片状态,2.点击section展开或者收缩列表,也要从字典获取imageView,并根据当前的状态获取

// 此处解决imageView重复利用的BUG问题 给从池子里取出的imageView根据状态,对其状态条件取反(即当前关闭取反石达开,因为点击之后状态要改变)重新设置图片形状,然后添加动画效果才有动画效果(如果不重新设置图片状态没有动画效果)

   if (self.stateDict[@(section)]) {// 如果section组是打开的让从池子随即取出的图片顺时针旋转(图像本身)90°

        targetImageView.transform =CGAffineTransformMakeRotation(M_PI_2);

    }else{

        targetImageView.transform =CGAffineTransformIdentity;// 如果section组是关闭的是使图片置为原始图片状态

    }

    

   return hfVeiw;

}


#pragma mark - clickAction:

- (void)clickAction:(UIButton *)button{

   NSInteger section = button.tag;

   BOOL isOpen = (self.stateDict[@(section)] ==nil);//isOpen default is NO

   if (isOpen) {

        [self.stateDictsetObject:@(1)forKey:@(section)];

    }else{

        [self.stateDictremoveObjectForKey:@(section)];

    }

    NSLog(@"----------%li,%@,%i",section,self.stateDict[@(section)],isOpen);

    

   NSIndexSet * set = [NSIndexSetindexSetWithIndex:section];

    

    [self.tabelViewreloadSections:set withRowAnimation:UITableViewRowAnimationFade];//

    

    // 设置动画

    // 通过字典获取对应的imageView

   UIImageView * imageView = self.imageDict[@(section)];

    

   if (!isOpen) {// 如果下拉列表是关闭的,就打开它(图片顺时针旋转90)

        imageView.transform =CGAffineTransformMakeRotation(M_PI_2);

    }else{// 如果下拉列表是打开的,就关闭它(图片置为原始状态)

        imageView.transform =CGAffineTransformIdentity;//恢复到原始状态

    }// 以上if解决图片没有动画效果

    

    [UIViewanimateWithDuration:0.3animations:^{

        // 重新判断状态

       if (isOpen) {

            imageView.transform =CGAffineTransformMakeRotation(M_PI_2);

        }else{

            imageView.transform =CGAffineTransformIdentity;

        }

    }];

    

}


#pragma mark - 返回每组的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   id temp = self.dataArray[section];

   NSArray * array = [temp objectForKey:@"friends"];

    

   if (self.stateDict[@(section)]!=nil) {

       return array.count;

    }

    

   NSLog(@"************%@",self.stateDict[@(section)]);

     return 0;

}


#pragma mark - cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString * identy =@"myTable";

   UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identy];

   if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identy];//UITableViewCellStyleSubtitle(选择有副标题的,以添加副标题)

    }

    // 增加标题

   id temp = self.dataArray[indexPath.section];

   NSArray * section = [temp objectForKey:@"friends"];

    cell.textLabel.text = section[indexPath.row];

    

    // 增加头像

    NSString * imageName = [NSStringstringWithFormat:@"head%d",arc4random_uniform(7)+1];

    cell.imageView.image = [UIImageimageNamed:imageName];

    

    // 增加子标题

    cell.detailTextLabel.text =@"在线";

    

   return cell;

}


@end


i QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值