最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS實(shí)現(xiàn)簡(jiǎn)易版的QQ下拉列表

 更新時(shí)間:2016年08月08日 17:11:47   投稿:daisy  
在我們?nèi)粘i_發(fā)中tableView是用的非常多的控件, 無論在新聞應(yīng)用, 視頻, 聊天應(yīng)用中都廣泛使用, 那么今天小編也分享一個(gè)用tableView實(shí)現(xiàn)的類似QQ界面的下拉列表.效果很簡(jiǎn)單,有需要的朋友們可以參考借鑒。

下面我們通過實(shí)例代碼來一步步看怎么實(shí)現(xiàn), 首先建立了兩個(gè)模型類, 一個(gè)Friend, 一個(gè)FriendGroup類. 數(shù)據(jù)源用的本地的一個(gè)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類的實(shí)例對(duì)象
@property (nonatomic, copy) NSMutableArray *friends;
// 用來判斷分組是否打開(opened屬性正是實(shí)現(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]; 
 }
 //重新對(duì)friends屬性數(shù)組賦值,此時(shí)存的都是Friend對(duì)象
 self.friends = [NSMutableArray arrayWithArray:tempArray];
}
@end

在ViewController中創(chuàng)建一個(gè)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的實(shí)例對(duì)象
@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必須實(shí)現(xiàn)的協(xié)議方法
- (void)touchAction:(SectionView *)sectionView {

}
#pragma mark - TableView Delegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 FriendGroup *friendGroup = [self.allArray objectAtIndex:section];
 //放一個(gè)封裝的view,view上有一個(gè)label和imageVIew,自帶touch事件,點(diǎn)擊觸發(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)建了一個(gè)tableView. 并根據(jù)數(shù)組個(gè)數(shù)給分區(qū)數(shù)量賦值, 然后在tableView: viewForHeaderInSection:方法里, 用一個(gè)自定的view給分區(qū)頭視圖賦值. 在tableView: cellForRowAtIndexPath:方法里給每個(gè)分區(qū)對(duì)應(yīng)的cell進(jìn)行了賦值. 先看一下效果.

從上圖可以看到現(xiàn)在每個(gè)分區(qū)中對(duì)應(yīng)有不同數(shù)量的row,但是還沒有實(shí)現(xiàn)我們想要的效果.所以再往下繼續(xù)看.

SectionView.m

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.delegate touchAction:self];
}
/*
 [self.delegate touchAction:self];
 協(xié)議方法會(huì)刷新tableview,然后會(huì)刷新tableview的 viewForHeaderInSection:方法
 就會(huì)重新布局SectionView所以會(huì)走layoutSubviews方法
 */
-(void)layoutSubviews
{
 [super layoutSubviews];
// 改變imageView的transform屬性 點(diǎn)擊時(shí)有開閉的效果
 [UIView animateWithDuration:0.3 animations:^{
  _imageView.transform = _group.opened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
 }];
}

點(diǎn)擊SectionView時(shí) 就讓代理人去執(zhí)行協(xié)議方法,但是在VC的協(xié)議方法中什么都沒寫, 所以需要完善一下

- (void)touchAction:(SectionView *)sectionView {
// 通過前面設(shè)置的tag值找到分區(qū)的index
 NSInteger index = sectionView.tag - 1000;
 FriendGroup *group = [self.allArray objectAtIndex:index];
// 每次點(diǎn)擊, 狀態(tài)變?yōu)榕c原來相反的值
 group.opened = !group.isOpened;
 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];
}

我們平時(shí)用的QQ下拉列表, 未打開時(shí)不顯示好友, 打開后才展示好友列表. 所以應(yīng)該在numberOfRowsInSection方法中要進(jìn)行設(shè)置.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 FriendGroup *group = [self.allArray objectAtIndex:section];
// 如果未打開 count為0 如果打開 count為group的屬性數(shù)組對(duì)應(yīng)的個(gè)數(shù)
 NSInteger count = group.isOpened ? group.friends.count : 0;
 return count;
}

效果如下圖

總結(jié)

以上就是IOS實(shí)現(xiàn)簡(jiǎn)易版的QQ下拉列表的全部?jī)?nèi)容,效果雖然很簡(jiǎn)單,但還會(huì)希望對(duì)大家開發(fā)IOS有所幫助。

相關(guān)文章

  • iOS CoreMotion實(shí)現(xiàn)設(shè)備運(yùn)動(dòng)加速度計(jì)陀螺儀

    iOS CoreMotion實(shí)現(xiàn)設(shè)備運(yùn)動(dòng)加速度計(jì)陀螺儀

    這篇文章主要介紹了iOS CoreMotion實(shí)現(xiàn)設(shè)備運(yùn)動(dòng)加速度計(jì)陀螺儀,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • iOS實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場(chǎng)動(dòng)畫

    iOS實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場(chǎng)動(dòng)畫

    這篇文章主要給大家介紹了利用iOS如何實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場(chǎng)動(dòng)畫,文中給出了詳細(xì)步驟實(shí)現(xiàn)代碼,對(duì)大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • iOS仿Uber篩選欄效果

    iOS仿Uber篩選欄效果

    這篇文章主要為大家詳細(xì)介紹了iOS仿Uber篩選欄的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 更新了Xcode8 及 iOS10遇到的問題小結(jié)

    更新了Xcode8 及 iOS10遇到的問題小結(jié)

    更新了Xcode8 以及 iOS10,App訪問用戶的相機(jī)、相冊(cè)、麥克風(fēng)、通訊錄的權(quán)限都需要重新進(jìn)行相關(guān)的配置,不然在Xcode8中打開編譯的話會(huì)直接crash.這篇文章主要介紹了更新了Xcode8 及 iOS10遇到的問題小結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS中 valueForKeyPath常用用法

    iOS中 valueForKeyPath常用用法

    這篇文章主要介紹了iOS valueForKeyPath常用用法,valueForKeyPath可以獲取數(shù)組中的最小值、最大值、平均值、求和。具體實(shí)例代碼大家參考下本文
    2018-08-08
  • IOS MenuViewController實(shí)現(xiàn)彈出菜單效果

    IOS MenuViewController實(shí)現(xiàn)彈出菜單效果

    這篇文章主要介紹了IOS MenuViewController實(shí)現(xiàn)彈出菜單效果,實(shí)現(xiàn)一個(gè)從下移動(dòng)上來的彈出菜單,背景逐漸變深,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • scrollview tableView嵌套解決方案示例

    scrollview tableView嵌套解決方案示例

    這篇文章主要介紹了scrollview tableView嵌套解決方案示例的代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • IOS開發(fā)實(shí)現(xiàn)錄音功能

    IOS開發(fā)實(shí)現(xiàn)錄音功能

    本文給大家分享的是一個(gè)IOS開發(fā)中實(shí)現(xiàn)錄音功能的實(shí)例,并簡(jiǎn)單給大家解析一下,有需要的小伙伴可以參考下
    2016-03-03
  • iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    這篇文章主要介紹了iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • IOS 開發(fā)自定義條形ProgressView的實(shí)例

    IOS 開發(fā)自定義條形ProgressView的實(shí)例

    這篇文章主要介紹了IOS 開發(fā)自定義條形ProgressView的實(shí)例的相關(guān)資料,希望開發(fā)自己的條形進(jìn)度條的朋友可以參考下
    2016-10-10

最新評(píng)論

武夷山市| 彩票| 顺义区| 嘉祥县| 耒阳市| 金塔县| 新巴尔虎右旗| 莒南县| 贵定县| 马山县| 方城县| 塔河县| 甘德县| 沾益县| 封开县| 望江县| 彭阳县| 咸宁市| 甘孜县| 游戏| 韶山市| 汝州市| 南溪县| 东至县| 山东省| 洞头县| 壤塘县| 临城县| 马边| 彭泽县| 弋阳县| 凌云县| 许昌市| 湘乡市| 民县| 双牌县| 神农架林区| 桦川县| 柳林县| 台东县| 江川县|