A. Requirements1. Using plist data, display similar QQ friends List group, group members display indentation function 2. Group name use header, Show arrow icon, group name, number of people in group and number of people on the line 3. Click Group name, Stretch, retract friend Group code Source:B. Implementation steps1. Writing the MVC structure(1) According to the plist file structure, write model, using nested type
1//2// FriendGroup.h 3// Friendslist 4//5// Created by Hellovoidworld on 14/12/12.6// Copyright ( c) 2014 Hellovoidworld. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 @class friend;12 @interface friendgroup:nsobject14 15/** Good Friend Group */16 @property (nonatomic, strong) Nsarray *friends;17 18/** friend Group name */19 @property (nonatomic, copy) NSString *name;20 21 /** Online number */22 @property (nonatomic, assign) int online;23-(instancetype) Initwithdictionary: (Nsdictionary *) Dictionar Y;25 + (Instancetype) friendgroupwithdictionary: (nsdictionary *) dictionary;26 @end
(2) Custom header for group name display, including arrow icon, group name, number of people in online/Group A. Data source: Friendgroup model
1/** Load Data */2-(void) Setfriendgroup: (Friendgroup *) Friendgroup
B. The location dimension information for the header control in the Init method is not initialized, and the Init method is used only to allocate memory, so use the initialized header location dimension data to use in the Layoutsubviews method
1/** child control Layout Method 2 When Init, only allocated memory, no initialization control size, where the header already has a position size of 3 */4-(void) Layoutsubviews {5 // The parent class must first be called Method 6 [Super Layoutsubviews]; 7 8 //1. Background 9 self.headerButtonView.frame = self.bounds;10 One //2. Number of people online/total number of people in the group cgfloat countwidth = 150;13 cgfloat countheight = self.frame.size.height;14 CGFloat countx = self.frame.size.width-10-countwidth;15 cgfloat County = 0;16 self.onlineCountView.frame = C Grectmake (Countx, county, Countwidth, countheight); 17}
C. Using the TableView cache pool, implement the corresponding DataSource method in the controller
1/** Customize the head of each section */2-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section {3< C0/>friendheader *header = [Friendheader friendheaderwithtableview:self.tableview];4 header.friendGroup = Self.friendgroups[section];5 return Header;6}
(3) Define cell to display each friend's information, including Avatar, nickname, Introduction (Signature) Implementation method the same as the header data source: Friend model here cell does not calculate the position size of the child control, directly using Uitableviewcellstylesubtitle type of cell
1/** Custom Construction Method */2 + (Instancetype) Cellwithtableview: (UITableView *) TableView {3 static nsstring *id = @ "Friendcell "; 4 Friendcell *cell = [TableView dequeuereusablecellwithidentifier:id]; 5 6 if (nil = = cell) {7 cell = [[s] Elf Alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; 8 } 9 return cell;11}12 13/** Load Data */14-(void) Setfrienddata: (Friend *) frienddata { _frienddata = frienddata;16 self.imageView.image = [UIImage imagenamed:frienddata.icon];18 Self.textLabel.text = frienddata.name;19 Self.textLabel.textColor = frienddata.isvip?[ Uicolor redcolor]:[uicolor blackcolor];20 self.detailTextLabel.text = frienddata.intro;21}
You can initially display the entire layout:2. Friends in the scaling group(1) in the group model, create an identity that identifies whether the friends of this group are displayed
1/** Whether stretch show Friends */2 @property (nonatomic, assign, getter=isopened) BOOL opened;
(2) Add a click event to the header to change the extension logo
1 //1.4 Click event 2 [Headerbuttonview addtarget:self Action: @selector (headerclicked) forControlEvents: UIControlEventTouchUpInside];
1/** Click event */2-(void) headerclicked {3 //1. Stretch, Hide group Friends 4 self.friendGroup.opened =!self.friendgroup.isopened; 5 6 //2. Refresh TableView 7 if ([Self.delegate respondstoselector: @selector (friendheaderdidclickedheader:)] ) {8 [self.delegate friendheaderdidclickedheader:self]; 9 }10}
(3) Writing an agent protocol to the header
1 @protocol friendheaderdelegate <nsobject>2/** Header clicked Proxy method */3 @optional4-(void) Friendheaderdidclickedheader: (Friendheader *) header;5 6 @end
(4) Viewcontroller determines whether to return the row number of the current group according to the group model's extension identifier
1-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {2 friendgroup *group = self.friendgroups[section];3 //Check the extension identifier within the model data 4 return group.isopened? Group.friends.count:0;5}
(5) Use Viewcontroller as the agent, monitor the header's Click events, refresh the TableView data
1 #pragma mark-friendheaderdelegate Method 2-(void) Friendheaderdidclickedheader: (Friendheader *) header {3 //Refresh Data 4 [Self.tableview Reloaddata];5}
3. Change the group's extension logo-arrow facing (1) since the TableView Reloaddata refresh interface and data are used in the proxy of the header, TableView will create all the headers from the new So it makes no sense to change the appearance of the header before Reloaddata (such as the arrow facing). So you need to modify the newly created header
1/**2 is added to the parent control 3 since TableView refreshes the data, all headers are re-created, so here are the arrows to make changes to the direction 4 */5-(void) Didmovetosuperview {6 // Change arrow direction, rotate clockwise 90 degrees 7 cgfloat rotation = self.friendGroup.isOpened? M_pi_2:0;8 self.headerButtonView.imageView.transform = cgaffinetransformmakerotation (rotation); 9}
(2) Use transform to rotate the arrows, because the original picture non-square and the fill way is stretched, need to do some property modification
1 //Change the arrow fill mode to center 2 [Headerbuttonview.imageview setcontentmode:uiviewcontentmodecenter];3 // You do not need to crop the bounds of the arrow picture 4 [Headerbuttonview.imageview Setclipstobounds:no];
"iOS basic controls-13" "Demo" QQ friends list TableView