Let's take a step-by-step look at how to implement through the example code, first set up two model classes, a friend, a Friendgroup class. A local plist file used by the data source. The plist file contains attributes such as the Friendgroup name,friends array.
Friend.h Sample Code
#import <Foundation/Foundation.h>
@interface friend:nsobject
@property (nonatomic, copy) NSString * Name;
@end
FriendGroup.h Sample Code
#import <Foundation/Foundation.h>
@interface friendgroup:nsobject
@property (nonatomic, copy) NSString *name;
An instance object stored in the array for the friend class
@property (nonatomic, copy) Nsmutablearray *friends;
Used to determine whether a group is open (the opened property is the key to implementing a Drop-down list)
@property (nonatomic, assign, getter = isopened) BOOL opened;
The custom method is used to assign a value
-(void) Setfriendgroupdic: (Nsmutabledictionary *) dic;
@end
friendgroup.m Sample Code
#import "FriendGroup.h"
#import "Friend.h"
@implementation friendgroup
-(void) Setfriendgroupdic: ( Nsmutabledictionary *) dic
{
//The Friendgroup attribute is assigned to the value by the dictionary
[self setvaluesforkeyswithdictionary:dic];
Nsmutablearray *temparray = [Nsmutablearray array];
Traversal Friends Property Array for
(Nsmutabledictionary *dic in self.friends) {
Friend *friend = [[Friend alloc] init];
[Friend Setvaluesforkeyswithdictionary:dic];
[Temparray addobject:friend];
}
Re-assign a value to the Friends property array at which time the friend object
self.friends = [Nsmutablearray Arraywitharray:temparray] is saved;
@end
Create a TableView in Viewcontroller
#import "ViewController.h" #import "SectionView.h" #import "FriendGroup.h" #import "Friend.h" #define Ktableviewreuse @ " Reuse "@interface Viewcontroller () <uitableviewdelegate, Uitableviewdatasource, sectionviewdelegate> @property (Nonatomic, Strong)
UITableView *tableview;
The Friendgroup instance object is stored in the array @property (nonatomic, strong) Nsmutablearray *allarray;
@end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
Self.allarray =[nsmutablearray Array];
[Self creattableview];
[Self getData]; }-(void) Creattableview {self.tableview = [[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstylep
Lain];
_tableview.delegate = self;
_tableview.datasource = self;
[_tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:ktableviewreuse];
[Self.view Addsubview:_tableview];
}//Get Data-(void) GetData {nsstring *filepath = [[NSBundle mainbundle] pathforresource:@ "friends.plist" oftype:nil]; Nsarray *temparray = [Nsarray arraywitHcontentsoffile:filepath];
For (Nsmutabledictionary *dic in Temparray) {Friendgroup *friendgroup = [[Friendgroup alloc] init];
[Friendgroup Setfriendgroupdic:dic];
[Self.allarray Addobject:friendgroup];
} [Self.tableview Reloaddata]; }-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section {return;}//Sectionvie The protocol method that W must implement-(void) Touchaction: (Sectionview *) Sectionview {} #pragma mark-tableview Delegate-(UIView *) TableView: (uit Ableview *) TableView viewforheaderinsection: (nsinteger) Section {friendgroup *friendgroup = [Self.allarray
Objectatindex:section]; Put an encapsulated View,view on a label and ImageView, bring the touch event, click the Trigger protocol method Sectionview *sectionview = [[Sectionview alloc]
Initwithframe:cgrectmake (0, 0, 375, 50)];
Sectionview.delegate = self;
Sectionview.tag = section + 1000;
SectionView.textLabel.text = Friendgroup.name;
Sectionview.group = Friendgroup;
return sectionview; } #pragma mark-tableview DataSource-(Nsinteger) numberofsectiOnsintableview: (UITableView *) TableView {return _allarray.count;}-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: (Nsinteger) Section {return [_allarray[section] friends].count;}-(UITableViewCell *) TableView :(UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {UITableViewCell *cell = [TableView
Dequeuereusablecellwithidentifier:ktableviewreuse];
Friendgroup *friendgroup = _allarray[indexpath.section];
Friend *friend = Friendgroup.friends[indexpath.row];
Cell.textLabel.text = Friend.name;
return cell;
} #pragma mark-memory waring-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. } @end
You can see from the code above that a tableview has been created. and assign values to the number of partitions based on the number of arrays, and then in the tableView: viewForHeaderInSection:
method, assign values to the partition header view with a custom view. The tableView: cellForRowAtIndexPath:
cell corresponding to each partition is assigned in the method. Look at the effect first.
You can see from the image above that there should be a different number of row in each partition, but we haven't achieved the desired results yet. So go ahead and look at it.
sectionview.m
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[self.delegate touchaction:self];
}
/* [self.delegate touchaction:self];
The Protocol method refreshes the tableview and then refreshes the TableView viewforheaderinsection: The method will
rearrange sectionview so it will go Layoutsubviews method
.
-(void) Layoutsubviews
{
[super layoutsubviews];
Change the ImageView transform property when clicked has the effect of opening and closing
[UIView animatewithduration:0.3 animations:^{
_imageview.transform = _group.opened? Cgaffinetransformmakerotation (m_pi_2): cgaffinetransformmakerotation (0);
}];
}
Click Sectionview to let the agent to execute the protocol method, but in the VC protocol method of nothing written, so need to improve
-(void) Touchaction: (Sectionview *) Sectionview {
//) finds the index Nsinteger index of the partition by the tag value set in the preceding setting
= Sectionview.tag- 1000;
Friendgroup *group = [Self.allarray objectatindex:index];
Each click, the state changes to the original value of the opposite
group.opened =!group.isopened;
[Self.tableview Reloadsections:[nsindexset Indexsetwithindex:index] Withrowanimation:uitableviewrowanimationnone] ;
}
We usually use the QQ drop-down list, not open when not show friends, open to show Friends list. So you should set it in the numberOfRowsInSection
method.
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
Friendgroup *group = [Self.allarray objectatindex:section];
If count is not turned on 0 the number
nsinteger count = group.isopened? group.friends.count:0
If the property array with Count group is turned on return count;
}
The effect of the following figure
Summarize
The above is the simple version of iOS to achieve the QQ Drop-down list of the full content, although the effect is very simple, but will also want to help you develop iOS.