IOS: Grouped table View UITableView, can be collapsed and expanded

Source: Internet
Author: User

Although the table view can be grouped, if the contents of each row are too many after grouping, it looks more troublesome to look backwards. To solve this problem, you can collapse and expand the grouped rows. When collapsed, the contents of the line are hidden, and when expanded, the contents of the line are displayed.

when folding: after expansion:                       

The specific code is as follows:

1 #import "ViewController.h"2 3 @interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate>4@property (Weak, nonatomic) iboutlet UITableView *TableView;5@property (strong,nonatomic) Nsarray *provinces;6@property (strong,nonatomic) nsdictionary *cities;7@property (strong,nonatomic) Nsmutablearray *cellstates;8 @end9 Ten @implementationViewcontroller One  A- (void) Viewdidload { - [Super Viewdidload]; -     //Initialize theSelf.provinces =[Nsarray array]; -Self.cities =[[Nsdictionary alloc]init]; -Self. Cellstates =[Nsmutablearray ArrayWithCapacity:self.provinces.count]; -      +     //Loading Data -NSString *path = [[NSBundle mainbundle]pathforresource:@"Cities"OfType:@"plist"]; +Nsdictionary *dic =[[Nsdictionary Alloc]initwithcontentsoffile:path]; A      at     if(DIC) -     { -         //all the provinces -Self.provinces = [dic objectforkey:@"Provinces"]; -          -         //all the Cities inSelf.cities = [dic objectforkey:@"Cities"]; -     } to      +     //Each section is collapsed by default -      for(intI=0; i<self.provinces.count; i++) the     { *NSNumber *state =[NSNumber Numberwithbool:no]; $ [Self. Cellstates Addobject:state];Panax Notoginseng     } -      the     //set up data sources and proxies +Self.tableView.dataSource =Self ; ASelf.tableview.Delegate=Self ; the      + } -  $ #pragmaData source methods for Mark-tableview $ //How many groups are there? --(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView - { the     returnSelf.provinces.count; - }Wuyi //How many rows each group has the-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section - { Wu     if([Self. Cellstates[section] Boolvalue])//Expand the -     { About         //Take out all the cities $Nsarray *cities =[Self.cities Objectforkey:self.provinces[section]]; -         returnCities.count; -     } -     Else //Folding of A     { +         return 0; the     } - } $ //set the contents of each cell the-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath the { the     //1. According to Reuseidentifier, first go to the object pool to find the reused Cell object the     StaticNSString *reuseidentifier =@"Citiescell"; -UITableViewCell *cell =[Self.tableview Dequeuereusablecellwithidentifier:reuseidentifier]; in     //2. If not found, create a Cell object yourself the     if(Cell = =Nil) the     { AboutCell =[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier]; the     } the     //3. Set the contents of a Cell object the     //Take out all the cities +Nsarray *cities =[Self.cities Objectforkey:self.provinces[indexpath.section]]; -Cell.textLabel.text =Cities[indexpath.row]; the     //Set Font ColorBayiCell.textLabel.textColor =[Uicolor Orangecolor]; the      the     returncell; - } - //Set Head Header the-(nsstring*) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section the { the     returnSelf.provinces[section]; the } - #pragmaProxy methods for Mark-tableview the-(uiview*) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) section the { theUIButton *button =[[UIButton alloc]init];94      the     //Set Title the [button settitle:self.provinces[section] forstate:uicontrolstatenormal]; the     98     //Set Color About [button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; -     101     //Set Alignment102Button.contenthorizontalalignment =Uicontrolcontenthorizontalalignmentleft;103     104     //Set Font size theButton.titleLabel.font = [Uifont systemfontofsize: -];106     107     //Add Event108 [button addtarget:self action: @selector (buttonclicked:) forcontrolevents:uicontroleventtouchupinside];109      the     //remember the button's tag111Button.tag =Section ; the     113     returnbutton; the } the  the #pragmaThe Mark-button event response117-(void) ButtonClicked: (uibutton*) Sender118 {119     //1. Remove the Old State -NSNumber *oldstate =[Self. Cellstates ObjectAtIndex:sender.tag];121     122     //2. Create a new state123NSNumber *newstate = [NSNumber numberwithdouble:![Oldstate Boolvalue]];124      the     //3. Delete the old State126 [Self. Cellstates RemoveObjectAtIndex:sender.tag];127      -     //4. Add a new state129 [Self. Cellstates insertobject:newstate AtIndex:sender.tag]; the     131     //Refresh Table the [Self.tableview Reloaddata];133 }134 @end

IOS: Grouped table View UITableView, can be collapsed and expanded

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.