IOS實現(xiàn)簡易版的QQ下拉列表
下面我們通過實例代碼來一步步看怎么實現(xiàn), 首先建立了兩個模型類, 一個Friend, 一個FriendGroup類. 數(shù)據(jù)源用的本地的一個plist文件. plist文件中包含了FriendGroup的name,friends數(shù)組等屬性.
Friend.h 示例代碼
#import <Foundation/Foundation.h> @interface Friend : NSObject @property (nonatomic, copy) NSString *name; @end
FriendGroup.h 示例代碼
#import <Foundation/Foundation.h> @interface FriendGroup : NSObject @property (nonatomic, copy) NSString *name; // 數(shù)組中存放的為Friend類的實例對象 @property (nonatomic, copy) NSMutableArray *friends; // 用來判斷分組是否打開(opened屬性正是實現(xiàn)下拉列表的關(guān)鍵) @property (nonatomic, assign, getter = isOpened) BOOL opened; // 自定義方法用來賦值 -(void)setFriendGroupDic:(NSMutableDictionary *)dic; @end
FriendGroup.m 示例代碼
#import "FriendGroup.h" #import "Friend.h" @implementation FriendGroup -(void)setFriendGroupDic:(NSMutableDictionary *)dic { // 通過字典給FriendGroup的屬性賦值 [self setValuesForKeysWithDictionary:dic]; NSMutableArray *tempArray = [NSMutableArray array]; // 遍歷friends屬性數(shù)組 for (NSMutableDictionary *dic in self.friends) { Friend *friend = [[Friend alloc] init]; [friend setValuesForKeysWithDictionary:dic]; [tempArray addObject:friend]; } //重新對friends屬性數(shù)組賦值,此時存的都是Friend對象 self.friends = [NSMutableArray arrayWithArray:tempArray]; } @end
在ViewController中創(chuàng)建一個tableView
#import "ViewController.h" #import "SectionView.h" #import "FriendGroup.h" #import "Friend.h" #define kTableViewReuse @"reuse" @interface ViewController ()<UITableViewDelegate, UITableViewDataSource, SectionViewDelegate> @property (nonatomic, strong) UITableView *tableView; // 數(shù)組中存放FriendGroup的實例對象 @property (nonatomic, strong) NSMutableArray *allArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.allArray =[NSMutableArray array]; [self creatTableView]; [self getData]; } - (void)creatTableView { self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse]; [self.view addSubview:_tableView]; } // 獲取數(shù)據(jù) - (void)getData { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]; NSArray *tempArray = [NSArray arrayWithContentsOfFile:filePath]; for (NSMutableDictionary *dic in tempArray) { FriendGroup *friendGroup = [[FriendGroup alloc] init]; [friendGroup setFriendGroupDic:dic]; [self.allArray addObject:friendGroup]; } [self.tableView reloadData]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; } // SectionView必須實現(xiàn)的協(xié)議方法 - (void)touchAction:(SectionView *)sectionView { } #pragma mark - TableView Delegate -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { FriendGroup *friendGroup = [self.allArray objectAtIndex:section]; //放一個封裝的view,view上有一個label和imageVIew,自帶touch事件,點擊觸發(fā)協(xié)議方法 SectionView *sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)]; sectionView.delegate = self; sectionView.tag = section + 1000; sectionView.textLabel.text = friendGroup.name; sectionView.group = friendGroup; return sectionView; } #pragma mark - TableView DataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _allArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_allArray[section] friends].count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse]; FriendGroup *friendGroup = _allArray[indexPath.section]; Friend *friend = friendGroup.friends[indexPath.row]; cell.textLabel.text = friend.name; return cell; } #pragma mark - Memory Waring - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
可以從上面代碼看到, 創(chuàng)建了一個tableView. 并根據(jù)數(shù)組個數(shù)給分區(qū)數(shù)量賦值, 然后在tableView: viewForHeaderInSection:
方法里, 用一個自定的view給分區(qū)頭視圖賦值. 在tableView: cellForRowAtIndexPath:
方法里給每個分區(qū)對應(yīng)的cell進行了賦值. 先看一下效果.
從上圖可以看到現(xiàn)在每個分區(qū)中對應(yīng)有不同數(shù)量的row,但是還沒有實現(xiàn)我們想要的效果.所以再往下繼續(xù)看.
SectionView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.delegate touchAction:self]; } /* [self.delegate touchAction:self]; 協(xié)議方法會刷新tableview,然后會刷新tableview的 viewForHeaderInSection:方法 就會重新布局SectionView所以會走layoutSubviews方法 */ -(void)layoutSubviews { [super layoutSubviews]; // 改變imageView的transform屬性 點擊時有開閉的效果 [UIView animateWithDuration:0.3 animations:^{ _imageView.transform = _group.opened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0); }]; }
點擊SectionView時 就讓代理人去執(zhí)行協(xié)議方法,但是在VC的協(xié)議方法中什么都沒寫, 所以需要完善一下
- (void)touchAction:(SectionView *)sectionView { // 通過前面設(shè)置的tag值找到分區(qū)的index NSInteger index = sectionView.tag - 1000; FriendGroup *group = [self.allArray objectAtIndex:index]; // 每次點擊, 狀態(tài)變?yōu)榕c原來相反的值 group.opened = !group.isOpened; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone]; }
我們平時用的QQ下拉列表, 未打開時不顯示好友, 打開后才展示好友列表. 所以應(yīng)該在numberOfRowsInSection
方法中要進行設(shè)置.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { FriendGroup *group = [self.allArray objectAtIndex:section]; // 如果未打開 count為0 如果打開 count為group的屬性數(shù)組對應(yīng)的個數(shù) NSInteger count = group.isOpened ? group.friends.count : 0; return count; }
效果如下圖
總結(jié)
以上就是IOS實現(xiàn)簡易版的QQ下拉列表的全部內(nèi)容,效果雖然很簡單,但還會希望對大家開發(fā)IOS有所幫助。
相關(guān)文章
iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController
UIActivityViewController類是一個標準的view controller,通個使用這個controller,你的應(yīng)用程序就可以提供各種服務(wù)。本文給大家介紹iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController,感興趣的朋友一起學習吧2015-11-11IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法
在iOS開發(fā)中,有時會有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶打開藍牙或者WIFI,提醒用戶打開推送或者位置權(quán)限等,接下來通過本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下2016-02-02iOS CoreTelephony 實現(xiàn)監(jiān)聽通話狀態(tài)
這篇文章主要介紹了iOS CoreTelephony 實現(xiàn)監(jiān)聽通話狀態(tài) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07