Original: http://blog.csdn.net/youcanping2008/article/details/9202167
First, the realization principle: when clicking on the table group header view, if the group of the table view is expanded, the reorganized row is set to 0, and if the group is hidden, all rows of that group are displayed. The effect is as follows:
Ii. implementation Step 1, defining a data model for encapsulating data[CPP]View Plaincopy
- #import <Foundation/Foundation.h>
- @interface Mydata:nsobject
- {
- Nsmutablearray *_array; //per group of data
- BOOL _isshow; //Group status, yes display group, no not display group
- NSString *_name; //Group name
- }
- @property (Nonatomic,retain) Nsmutablearray *array;
- @property (nonatomic,copy) nsstring * name;
- @property (nonatomic,assign) BOOL isshow;
- @end
2. Add a data source[CPP]View Plaincopy
- -(void) Viewdidload
- {
- [Super Viewdidload];
- additional setup after loading the view.
- //Global Data source
- DataArray = [[Nsmutablearray alloc] init];
- //Add Data
- For (int i=' A '; i<=' Z '; i++) {
- MyData *mydata = [[MyData alloc] init];
- Mydata.name = [NSString stringwithformat:@"%c", I];
- For (int j=0; j<10; J + +) {
- [Mydata.array addobject:[nsstring stringwithformat:@"%c-%d", I,j]];
- }
- [DataArray Addobject:mydata];
- }
- Mytableview = [[UITableView alloc] Initwithframe:cgrectmake (0, 0, +, 460-44) Style:uitableviewstyleplain];
- Mytableview.datasource = self;
- Mytableview.delegate = self;
- Mytableview.rowheight = 30;
- [Self.view Addsubview:mytableview];
- }
3. Proxy method for implementing a table
[CPP]View Plaincopy
- Number of groups
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
- {
- return [DataArray Count];
- }
- Depending on the state to determine whether the group is displayed, the hidden group sets the number of rows in the group to 0
- -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
- {
- MyData *data = [DataArray objectatindex:section];
- if ([data isshow]) {
- return [[Data array] count];
- }else{
- return 0;
- }
- }
- Add content to be displayed for each row
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- static NSString *cellname = @"Cell";
- UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellname];
- if (!cell) {
- cell = [[[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellname] autorelease];
- }
- MyData *data = [DataArray objectAtIndex:indexPath.section];
- NSString *STR = [[Data array] objectAtIndex:indexPath.row];
- Cell.textLabel.text = str;
- return cell;
- }
4. Customize the header view of the group, add click events[CPP]View Plaincopy
- Define a view of header headers, add a click event
- -(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
- {
- MyData *data = [DataArray objectatindex:section];
- UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
- Btn.frame = CGRectMake (0, 0, 320, 30);
- [Btn SetTitle:data.name Forstate:uicontrolstatenormal];
- Btn.tag = section;
- [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
- if (section%2) {
- Btn.backgroundcolor = [Uicolor Darkgraycolor];
- }else{
- Btn.backgroundcolor = [Uicolor Lightgraycolor];
- }
- return BTN;
- }
- -(void) Btnclick: (UIButton *) btn
- {
- MyData *data = [DataArray ObjectAtIndex:btn.tag];
- //Change the display status of the group
- if ([data isshow]) {
- [Data Setisshow:no];
- }else{
- [Data Setisshow:yes];
- }
- //Refresh clicked group header, animate using card
- [Mytableview Reloadsections:[nsindexset IndexSetWithIndex:btn.tag] withrowanimation:uitableviewrowanimationfade];
- }
How does "Go" ios achieve the folding effect of a table?