How to fold a UITableView group and fold a uitableview Group

Source: Internet
Author: User

How to fold a UITableView group and fold a uitableview Group

I used the folding group when I was working on the project. I recently studied some online projects and found some shortcomings. I finally wrote one after several days of research. In addition, the data sources in the group are flexible, and the status between each group has no impact.

 

The general idea of implementation is that each group uses a section to save, row0 is used to save the group title, and the cell behind it stores the data of each group.

 

1. Create a model class to save the group information of each group, including the group name, cell in each group, and switch status of the current group.

//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 = @ "group"; 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. Use a viewController to manage 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: UITableViewS TyleGrouped]; self. sections = [[NSMutableArray alloc] init]; [self initData]; self. tableView. delegate = self; self. tableView. dataSource = self; // set the distance between each group to 0 self. tableView. sectionFooterHeight = 0; self. tableView. sectionHeaderHeight = 0; [self. view addSubview: self. tableView];} // initialize data-(void) initData {for (int I = 0; I <5; I ++) {MySection * section = [[MySection alloc] init]; section. name = [NSStrin G stringWithFormat: @ "group % I", I + 1]; [self. sections addObject: section] ;}}- (void) didReceiveMemoryWarning {[super didReceiveMemoryWarning];}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {MySection * theSection = self. sections [section]; // dynamically change the number of rows in each group based on the group switch status and data source if (theSection. isOpen) {return [theSection. dataArray count];} else {return 1 ;}// number of groups-(NSInte Ger) numberOfSectionsInTableView :( UITableView *) tableView {return [self. sections count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [[UITableViewCell alloc] init]; // retrieve the object MySection * section = self corresponding to the current cell from the data source array. sections [indexPath. section]; // if row is 0, it is the title if (indexPath. row = 0) {cell. textLabel. text = section. name; Cell. backgroundColor = [UIColor grayColor];} else {// assign cell values to cells in each group. textLabel. text = section. dataArray [indexPath. row-1];} return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {MySection * section = self. sections [indexPath. section]; // select the title cell, and the corresponding group does not open 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 endU Pdates];} else if (indexPath. row = 0) {// The group corresponding to the selected cell has been opened, and the selected 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] ;}// determines whether the title is cel. L set the row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {if (indexPath. row! = 0) {return 77;} else {return 44; }}@ end

3. There is nothing special in AppDelegate, but paste it.

//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

In this way, you can obtain the data source as needed. However, the blogger is also a newbie who has just been studying for a while. I hope you can correct the mistakes and make progress together.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.