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

發佈了62 篇原創文章 · 獲贊 3 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章