欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)中TableView類似QQ分組的折疊與展開效果

 更新時間:2016年12月29日 11:27:22   作者:花落的花  
這篇文章主要介紹了iOS開發(fā)中TableView類似QQ分組的折疊與展開效果,其實要做這個效果我先想到的是在tableView中再嵌套多個tableView。下面通過本文給大家分享實現(xiàn)思路,需要的朋友可以參考下

類似QQ分組的樣子,實現(xiàn)tableView的折疊與展開。其實要做這個效果我先想到的是在tableView中再嵌套多個tableView,這個想法實現(xiàn)起來就有點難了。

所以還是換個思路,把tableView的HeaderView用上了。給headerView加上手勢,輕松解決折疊展開的問題。

直接上代碼吧。

@property (nonatomic, strong) UITableView *myTableView; 
@property (nonatomic, strong) NSMutableArray *listArray;  // 數(shù)據(jù)源
@property (nonatomic, strong) NSMutableArray *titlesArray;  // 分組的名稱
@property (nonatomic, strong) NSMutableDictionary *openSectionDict; // 記錄哪個組展開
- (void)viewDidLoad {
 [super viewDidLoad];
 // 初始化tableView
 _myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
 self.myTableView.delegate = self;
 self.myTableView.dataSource = self;
 [self.view addSubview:_myTableView];
 self.openSectionDict = [[NSMutableDictionary alloc] init]; // 初始化字典
 [self setUpData];
}
// 給數(shù)據(jù)源賦值
- (void)setUpData {
 self.listArray = [NSMutableArray new];
 self.titlesArray = [NSMutableArray new];
 for (int i = 0; i < 5; i++) {  // 5個section
  [self.titlesArray addObject:[NSString stringWithFormat:@"section %d", i]];
  NSMutableArray *array = [NSMutableArray new];
  for (int i = 0; i < 4; i++) { // 每個section有4個row
   [array addObject:[NSString stringWithFormat:@"row %d", i]];
  }
  [self.listArray addObject:array];
 }
}
// 實現(xiàn)tableView的代理方法
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) { //根據(jù)記錄的展開狀態(tài)設(shè)置row的數(shù)量
  return 0;
 } else {
  return 4;
 }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
 if (!cell) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL_ID"];
  cell.textLabel.text = [NSString stringWithFormat:@"row %ld", indexPath.row];
 }
 return cell;
}
#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
 return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
 view.backgroundColor = [UIColor whiteColor];
 view.tag = KTAG + section;
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, view.bounds.size.width, view.bounds.size.height)];
 label.text = self.titlesArray[section];
 [view addSubview:label];
 if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) {
  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 10) / 2, 7, 10)];
  imageView.image = [UIImage imageNamed:@"Triangle_right_gray"]; // 三角形小圖片
  [view addSubview:imageView];
 } else {
  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 7) / 2, 10, 7)];
  imageView.image = [UIImage imageNamed:@"Triangle_down_gray"];
  [view addSubview:imageView];
 }
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collegeTaped:)];
 [view addGestureRecognizer:tap];
 return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
 return 0.1;
}
#pragma mark - sectionHeader clicked
- (void)collegeTaped:(UITapGestureRecognizer *)sender {
 NSString *key = [NSString stringWithFormat:@"%ld", sender.view.tag - KTAG];
 // 給展開標識賦值
 if ([[self.openSectionDict objectForKey:key] integerValue] == 0) {
  [self.openSectionDict setObject:@"1" forKey:key];
 } else {
  [self.openSectionDict setObject:@"0" forKey:key];
 }
 NSUInteger index = sender.view.tag;
 NSIndexSet *set = [NSIndexSet indexSetWithIndex:index - KTAG];
 [self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

最后的效果:

以上所述是小編給大家介紹的iOS開發(fā)中TableView類似QQ分組的折疊與展開效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 基于Android實現(xiàn)仿QQ5.0側(cè)滑

    基于Android實現(xiàn)仿QQ5.0側(cè)滑

    本課程將帶領(lǐng)大家通過自定義控件實現(xiàn)QQ5.0側(cè)滑菜單,課程將循序漸進,首先實現(xiàn)最普通的側(cè)滑菜單,然后引入屬性動畫與拖動菜單效果相結(jié)合,最終實現(xiàn)QQ5.0側(cè)滑菜單效果。通過本課程大家會對側(cè)滑菜單有更深層次的了解,通過自定義控件和屬性動畫打造千變?nèi)f化的側(cè)滑菜單效果
    2015-12-12
  • Android Menu詳解及示例代碼

    Android Menu詳解及示例代碼

    本文主要介紹Android Menu,這里對Android菜單(menu)進行了詳細的介紹,并給出示例代碼和實現(xiàn)效果圖,有需要的小伙伴可以參考下
    2016-08-08
  • Android SDK Manager解決更新時的問題 :Failed to fetch URL...

    Android SDK Manager解決更新時的問題 :Failed to fetch URL...

    本文主要介紹解決安裝使用SDK Manager更新時的問題:Failed to fetch URL...,這里提供了詳細的資料及解決問題辦法,有需要的小伙伴可以參考下
    2016-09-09
  • Android使用PowerImageView實現(xiàn)播放強大的ImageView動畫效果

    Android使用PowerImageView實現(xiàn)播放強大的ImageView動畫效果

    今天我們就來編寫一個PowerImageView控件,讓它既能支持ImageView控件原生的所有功能,同時還可以播放GIF圖片
    2018-05-05
  • 詳解Android Webview加載網(wǎng)頁時發(fā)送HTTP頭信息

    詳解Android Webview加載網(wǎng)頁時發(fā)送HTTP頭信息

    這篇文章主要介紹了詳解Android Webview加載網(wǎng)頁時發(fā)送HTTP頭信息的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Flutter實現(xiàn)支付寶集五福手畫福字功能

    Flutter實現(xiàn)支付寶集五福手畫福字功能

    支付寶一年一度的集五?;顒佑珠_始了,其中包含了一個功能就是手寫福字,還包括撤銷一筆,清除重寫,保存相冊等。本文將介紹如何使用Flutter實現(xiàn)這些功能,感興趣的可以了解一下
    2022-01-01
  • Android開發(fā)MQTT協(xié)議的模型及通信淺析

    Android開發(fā)MQTT協(xié)議的模型及通信淺析

    這篇文章主要W為大家介紹了Android開發(fā)MQTT協(xié)議的模型及通信淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 詳解在Flutter中如何使用dio

    詳解在Flutter中如何使用dio

    應(yīng)用程序開發(fā)的一個關(guān)鍵部分是優(yōu)雅地處理網(wǎng)絡(luò)請求。網(wǎng)絡(luò)返回的響應(yīng)可能包含意想不到的結(jié)果,為了獲得良好的用戶體驗,您需要提前處理邊緣情況。本文將詳細為大家介紹Flutter如何使用dio,需要的可以參考一下
    2022-04-04
  • Android自定義webView頭部進度加載效果

    Android自定義webView頭部進度加載效果

    這篇文章主要介紹了Android自定義webView頭部進度加載效果,小編畫一條進度線,然后加載webview上面,具體實現(xiàn)代碼大家參考下本文
    2017-11-11
  • android實現(xiàn)驗證碼按鈕

    android實現(xiàn)驗證碼按鈕

    這篇文章主要為大家詳細介紹了android實現(xiàn)驗證碼按鈕功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論