摺疊UITableView分組實現方法,摺疊uitableview分組
做項目的時侯用到了摺疊分組,最近就研究了一下,找了一些網上的項目,發現有一些缺點,研究了幾天自己終於寫出了一個。而且分組的資料來源比較靈活,每組之間的狀態沒有什麼影響。
實現的大體思路是每個分組用一個section來儲存,row0用來儲存分組的標題,後面的cell儲存每個分組的資料。
1.首先要建立一個儲存每組的分組資訊的model類,包括分組名,每組裡面的cell,和當前組的開關狀態。
//MySection.h#import <Foundation/Foundation.h>@interface MySection : NSObject@property (nonatomic) BOOL isOpen;@property (nonatomic) NSMutableArray *dataArray;@property (nonatomic) NSString *name;@end
//MySection.m#import "MySection.h"@interface MySection ()@end@implementation MySection- (instancetype)init{ self = [super init]; self.isOpen = false; self.name = @"分組"; self.dataArray = [[NSMutableArray alloc]init]; for (int i = 0; i < 5; i++) { NSString *string = [NSString stringWithFormat:@"NO.%i",i]; [self.dataArray addObject:string]; } return self;}@end
2.通過一個viewController來管理tableView。
//ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>@end
//ViewController.m#import "ViewController.h"#import "MySection.h"@interface ViewController ()@property (nonatomic) UITableView *tableView;@property (nonatomic) NSIndexPath *selectedIndexPath;@property (nonatomic) NSMutableArray *sections;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; self.sections = [[NSMutableArray alloc]init]; [self initData]; self.tableView.delegate = self; self.tableView.dataSource = self; //設定每組之間的距離為0 self.tableView.sectionFooterHeight = 0; self.tableView.sectionHeaderHeight = 0; [self.view addSubview:self.tableView];}//初始化資料- (void)initData{ for (int i = 0; i < 5; i++) { MySection *section = [[MySection alloc]init]; section.name = [NSString stringWithFormat:@"分組%i",i + 1]; [self.sections addObject:section]; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ MySection *theSection = self.sections[section]; //根據分組開關狀態和資料來源動態改變每組row的個數 if (theSection.isOpen) { return [theSection.dataArray count]; }else{ return 1; }}//分組數目- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [self.sections count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc]init]; //從資料來源數組中取出當前cell對應的對象 MySection *section = self.sections[indexPath.section]; //如果row為0,則為標題 if (indexPath.row == 0) { cell.textLabel.text = section.name; cell.backgroundColor = [UIColor grayColor]; }else{ //為每組中cell賦值 cell.textLabel.text = section.dataArray[indexPath.row - 1]; } return cell;}- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ MySection *section = self.sections[indexPath.section]; //選中標題cell,且對應的組沒有開啟 if (!section.isOpen) { NSLog(@"section:%@ open!",section.name); section.isOpen = YES; NSMutableArray *a = [[NSMutableArray alloc]init]; for (int i = 1; i < [section.dataArray count]; i++) { NSIndexPath *addIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; [a addObject:addIndexPath]; } [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:a withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates]; }else if (indexPath.row == 0){ //選中的cell對應的組已經開啟,且選中的是row0 NSLog(@"section:%@ close!",section.name); section.isOpen = !section.isOpen; NSMutableArray *b = [[NSMutableArray alloc]init]; for (int i = 1; i < [section.dataArray count]; i++) { NSIndexPath *redIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; [b addObject:redIndexPath]; } [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:b withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; } }//判斷是否為標題cell設定行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row != 0) { return 77; }else{ return 44; }}@end
3.AppDelegate裡面沒什麼特別的了,不過還是貼出來吧。
//AppDelegate.m#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc]init]; self.window.rootViewController = vc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}@end
這樣就大功告成了,可以根據情況擷取資料來源。不過博主也是剛學習了一段時間的菜鳥,有什麼錯誤希望大家指正,共同進步。