There are two styles of UITableView: Uitableviewstyleplain and uitableviewstylegrouped. There is no essential difference between the two, except that the latter is shown in the Normal style by the grouping style. Today we'll look at the use of groupings:
1, first we introduce the tableview of the group, the initialization of a tableview as follows
#pragmaMark-Load Table View-(void) loadtableview{_tableview=[[uitableview Alloc] Initwithframe:cgrectmake (0, -, Kwidth, Kheight) style:uitableviewstylegrouped]; //Set up proxy_tableview.Delegate=Self ; _tableview.datasource=Self ; //Set Row Height_tableview.rowheight= -; //hide the height of the grouping foot_tableview.sectionfooterheight=0; [Self.view Addsubview:_tableview];}
2, loading data, grouping data we have defined in the Plist file, the loading code is as follows:
#pragma mark-load data-(void) loaddata{ * path=[[nsbundle Mainbundle] Pathforresource: @" friends.plist " Oftype:nil]; _array=[Nsarray arraywithcontentsoffile:path]; }
3. Initialize Proxy method
#pragmaMark-Set number of groupings-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return_array.count;}#pragmaMark-Set the height of the grouping-(cgfloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{return +;}#pragmaMark-Custom group header-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) section{nsdictionary*dic=_array[section]; NSString* title=dic[@"Group"]; //1 Customizing the headUIView * view=[[UIView alloc] init]; View.backgroundcolor=[Uicolor Graycolor]; View.layer.borderWidth=1; View.layer.borderColor=[Uicolor Whitecolor]. Cgcolor; //2 Add buttonUIButton * button=[UIButton Buttonwithtype:uibuttontypecustom]; [Button Settitle:title Forstate:uicontrolstatenormal]; Button.frame=cgrectmake (0,0, Kwidth, +); Button.tag=Section ; [Button addtarget:self action: @selector (Clickthegroup:) forcontrolevents:uicontroleventtouchupinside]; [View Addsubview:button]; //3 Add arrows to the leftUiimageview * Imageview=[[uiimageview Alloc] Initwithframe:cgrectmake (5, +/2.0- -/2.0, -, -)]; Imageview.image=[uiimage imagenamed:@"Disclosure.png"]; Imageview.tag=101; [Button Addsubview:imageview]; [_headimageview Setobject:imageview forkey:@ (section)]; returnview;}#pragmamark-uitableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return 0; }
As follows:
4, we can click on a group to refresh the data, by controlling the number of data in the current group to achieve this effect, because the current grouping state has two closed and open, so we need to define a dictionary to control the state, the dictionary key is the index of the current grouping, the value of 1 is opened, A value of 2 is turned off. Each time we click, we need to reinitialize the current state, the current state changes when the corresponding group contains the number of data bars to 0
-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: ( Nsinteger) section{ [_state[@ ( section)] [intvalue]; Nsdictionary *dic=_array[section]; Nsarray * Friends=dic[@ " friends " if return Friends.count; else { return 0 ; } }
5, refresh need to control the rotation of the triangle icon, so we need to animate, to complete the current effect
#pragmaMark-click Group Info-(void) Clickthegroup: (UIButton *) button{intGroupindex= (int) Button.tag; intflag=0;//used to control the re-instantiation of the button if([_state[@ (Groupindex)] intvalue]==0) {[_state setobject:@ (1) forkey:@ (Groupindex)]; Flag=0; }Else{[_state setobject:@ (0) forkey:@ (Groupindex)]; Flag=1; } //refreshes the current groupingNsindexset *Set=[[Nsindexset alloc] initwithindex:groupindex]; [_tableview reloadsections:SetWithrowanimation:uitableviewrowanimationnone]; Uiimageview* imageview=_headimageview[@ (Groupindex)]; //simulated animations, re-refreshed each time so affine changes revert back to their original state. if(flag) {Imageview.transform=cgaffinetransformrotate (Imageview.transform, m_pi_2); } [UIView animatewithduration:0.3animations:^{ if(flag==0) {Imageview.transform=cgaffinetransformmakerotation (m_pi_2); }Else{imageview.transform=cgaffinetransformmakerotation (0); } }]; }
The following results are done:
Animation Instant Effect
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:
IOS UITableView Group List