How does "Go" ios achieve the folding effect of a table?

Source: Internet
Author: User

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
  1. #import <Foundation/Foundation.h>
  2. @interface Mydata:nsobject
  3. {
  4. Nsmutablearray *_array; //per group of data
  5. BOOL _isshow; //Group status, yes display group, no not display group
  6. NSString *_name; //Group name
  7. }
  8. @property (Nonatomic,retain) Nsmutablearray *array;
  9. @property (nonatomic,copy) nsstring * name;
  10. @property (nonatomic,assign) BOOL isshow;
  11. @end
2. Add a data source[CPP]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. additional setup after loading the view.
  5. //Global Data source
  6. DataArray = [[Nsmutablearray alloc] init];
  7. //Add Data
  8. For (int i=' A '; i<=' Z '; i++) {
  9. MyData *mydata = [[MyData alloc] init];
  10. Mydata.name = [NSString stringwithformat:@"%c", I];
  11. For (int j=0; j<10; J + +) {
  12. [Mydata.array addobject:[nsstring stringwithformat:@"%c-%d", I,j]];
  13. }
  14. [DataArray Addobject:mydata];
  15. }
  16. Mytableview = [[UITableView alloc] Initwithframe:cgrectmake (0, 0, +, 460-44) Style:uitableviewstyleplain];
  17. Mytableview.datasource = self;
  18. Mytableview.delegate = self;
  19. Mytableview.rowheight = 30;
  20. [Self.view Addsubview:mytableview];
  21. }

3. Proxy method for implementing a table


[CPP]View Plaincopy
  1. Number of groups
  2. -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
  3. {
  4. return [DataArray Count];
  5. }
  6. Depending on the state to determine whether the group is displayed, the hidden group sets the number of rows in the group to 0
  7. -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
  8. {
  9. MyData *data = [DataArray objectatindex:section];
  10. if ([data isshow]) {
  11. return [[Data array] count];
  12. }else{
  13. return 0;
  14. }
  15. }
  16. Add content to be displayed for each row
  17. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
  18. {
  19. static NSString *cellname = @"Cell";
  20. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellname];
  21. if (!cell) {
  22. cell = [[[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellname] autorelease];
  23. }
  24. MyData *data = [DataArray objectAtIndex:indexPath.section];
  25. NSString *STR = [[Data array] objectAtIndex:indexPath.row];
  26. Cell.textLabel.text = str;
  27. return cell;
  28. }

4. Customize the header view of the group, add click events[CPP]View Plaincopy
    1. Define a view of header headers, add a click event
    2. -(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
    3. {
    4. MyData *data = [DataArray objectatindex:section];
    5. UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
    6. Btn.frame = CGRectMake (0, 0, 320, 30);
    7. [Btn SetTitle:data.name Forstate:uicontrolstatenormal];
    8. Btn.tag = section;
    9. [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
    10. if (section%2) {
    11. Btn.backgroundcolor = [Uicolor Darkgraycolor];
    12. }else{
    13. Btn.backgroundcolor = [Uicolor Lightgraycolor];
    14. }
    15. return BTN;
    16. }
    17. -(void) Btnclick: (UIButton *) btn
    18. {
    19. MyData *data = [DataArray ObjectAtIndex:btn.tag];
    20. //Change the display status of the group
    21. if ([data isshow]) {
    22. [Data Setisshow:no];
    23. }else{
    24. [Data Setisshow:yes];
    25. }
    26. //Refresh clicked group header, animate using card
    27. [Mytableview Reloadsections:[nsindexset IndexSetWithIndex:btn.tag] withrowanimation:uitableviewrowanimationfade];
    28. }

How does "Go" ios achieve the folding effect of a table?

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.