The effect is as follows:
The implementation results are divided into two parts:
-
Data model
-
TableView
To implement TableView, you need to implement DataSource and delegate
DataSource main function is to show what data
The main function of delegate is to deal with event response.
The code is as follows:. h
Controller@interface Viewcontroller:uiviewcontroller <uitableviewdatasource, UITableViewDelegate>@ Property (Nonatomic, retain) Nsmutablearray *datalist; Data @property (nonatomic, retain) UITableView *tableview; @end
. m file
@implementation ViewController- (void) viewdidload { [super Viewdidload]; // do any additional setup after loading the view, typically from a nib. [self initdatalist] ; // Initialize data // Move Down 20 CGRect frame = self.view.frame; frame.origin.y = 20; frame.size.height = frame.size.height - 20; self.tableview = [[uitableview alloc] initwithframe:frame style: uitableviewstyleplain]; // setting up a delegate self.tableview.datasource = self; self.tableview.delegate = self; [self.view addsubvieW:self.tableview];} - (void) didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated.} #pragma mark - datasource implementation// altogether several section- (Nsinteger) Numberofsectionsintableview: ( uitableview *) Tableview { return [self.datalist count];} Number of groups under each section- (Nsinteger) TableView: (uitableview *) Tableview numberofrowsinsection: ( Nsinteger) Section { list *list = [self.datalist objectatindex: section]; nsmutablearray *array = list.groups; nsinteger all = [array count]; for (Group *group in array) { if (group.isdelop) { all = all + [group.friends count]; } } return All;} Show each cell- (uitableviewcell *) TableView: (uitableview *) tableview Cellforrowatindexpath: (nsindexpath *) indexpath { if ([Self isGroup: Indexpath]) { return [self getgroupcell: tableview and:indexpath]; // Grouping of cell } return [self getfriendcell:tableview and:indexpath]; / / Friends cell}/* * Get section title **/- (nsstring *) TableView: (uitableview *) Tableview titleforheaderinsection: (Nsinteger) section { list *list = [self.datalist objectatindex:secTion]; return list.title;} #pragma mark - Click on event// click Processing, if grouping expands, otherwise- (void) TableView is not processed: (uitableview *) Tableview didselectrowatindexpath: (nsindexpath *) indexpath { group *group = [self isgroup:indexpath]; // If you click on a group, take the inverse of the current state and update the data if (group) { group.isdelop = !group.isDelop; [self.tableView reloadData]; &NBSP;&NBSP;&NBSP}} #pragma mark - get the data of the click Location/* * the location is grouped, return the points grouped data **/- ( group *) IsGroup: (nsindexpath *) indexpath { int num = -1; NSUInteger index = indexPath.row; list *list = [_datalist objectatindex:indexpath.section]; nsmutablearray *groups = list.groups; for (group *group in groups) { num++; if (Index == num) { return group; } if (Group.isdelop) { num += [group.friends count]; if (Index <= num) { return nil; } } &nbSp; } return nil;} /* * whether the location is a friend, and return to the point of Friends Data **/- (friend *) Isfriend: (nsindexpath *) indexpath { int num = -1; NSUInteger index = indexpath.row; list *list = [_datalist objectatindex:indexpath.section]; // acquiring points of section nsmutablearray * groups = list.groups; // get all the groups under section for (group *group in groups) { num++; if (Group.isdelop) { int temp = num; num += [group.friends count]; if (Index <= num) { int k = index - temp - 1; return [group.friends objectAtIndex:k]; } } } return nil;} #pragma mark - Initialize cell/* * set cell **/- for grouping (uitableviewcell *) Getgroupcell: (uitableview *) Tableview and: (nsindexpath *) indexpath { group *nowgroup = [self isgroup:indexpath]; static nsstring *cellwithidentifier = @ "Groupcell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellwithidentifier]; if (Cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellwithidentifier]; cell.backgroundcolor = [uicolor colorwithred:227/250.0 green:168/250.0 blue:105/250.0 alpha:1]; } cell.textLabel.text = nowgroup.title; cell.detailtextlabel.text = nowgroup.detail; if (Nowgroup.isdelop) { cell.imageview.image = [uiimage imagenamed:@ "CollecT.png "]; } else { cell.imageview.image = [uiimage imagenamed:@ "Tomore.png"]; } return cell;} /* * set friend cell **/- (uitableviewcell *) Getfriendcell: (uitableview *) TableView and: (nsindexpath *) indexpath { friend *nowfriend = [self isfriend:indexpath]; static nsstring *cellwithidentifier = @ " Friendcell "; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellwithidentifier]; if (Cell == nil) { cell = [[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellwithidentifier]; }& Nbsp; // setting up cell data cell.textLabel.text = nowFriend.name; cell.detailTextLabel.text = nowFriend.state; Cell.imageview.image = [uiimage imagenamed:nowfriend.image]; return cell;} #pragma mark - size style settings- (cgfloat) TableView: (uitableview *) tableview Heightforrowatindexpath: (nsindexpath *) indexpath { if ([self Isgroup:indexpath]) { return 30; // Group High } return 40; // friend's Row height}- (cgfloat) TableView: (uitableview *) Tableview heightforheaderinsection: ( Nsinteger) section { return 20;} #pragma mark - displayed data/* * data initialization construction **/- (void) initdatalist { friend *friend1 = [[friend alloc] initwithimage:@ "Back1.jpg" name:@ "the Abyss of Misery" states:@ "[online] ah distressed!]"; friend *friend2 = [[friend alloc] initwithimage:@ "Back2.jpg" name:@ "Duan Tianya" states:@ "[Online] dearly!]"; friend *friend3 = [[friend alloc] initwithimage:@ "Back3.jpg" name:@ "Little Sister" states:@ "[Online] pain!"]; friend *friend4 = [[friend alloc] initwithimage:@ "Back4.jpg" name:@ "Call ha" states:@ "[Online] love dearly!"]; NSMutableArray *array1 = [NSMutableArray Arraywithobjects:friend1, friend2, friend3, friend4, nil]; group *group1 = [[group alloc] initwithtitle:@ "Family" delop:NO friends:array1]; group1.detail = @ "4/4"; friend *friend5 = [ [friend alloc] initwithimage:@ "Back5.jpg" name:@ "Duan" states:@ "[Online] distressed!]"; friend *friend6 = [[friend alloc] initwithimage:@ "Back6.jpg" name:@ "Small Merchant" states:@ "[Online] pain!]"; friend *friend7 = [[friend alloc] initwithimage:@ "Back7.jpg" name:@ "eldest brother" states:@ "[Offline] distressed!]"; NSMutableArray *array2 = [NSMutableArray Arraywithobjects:friend5, friend6, friend7, nil]; group *group2 = [[group alloc] initwithtitle:@ "Friends" delop:NO friends:array2]; group2.detail = @ "2/3"; nsmutablearray *l1 = [nsmutablearray arraywitHobjects:group1, group2, nil]; list *list1 = [[list alloc ] initwithtitle:@ "My Friends" groups:l1]; friend * mobile = [[friend alloc] initwithimage:@ "Back1.jpg" name:@ "My Phone" states:@ "[offline] Phone not online "]; nsmutablearray *array = [nsmutablearray arraywithobjects: Mobile, nil]; group *group = [[group alloc] initwithtitle : @ "My Device" delop:no friends:array]; group.detail = @ "0/1"; NSMutableArray *l2 = [NSMutableArray Arraywithobjects:group, nil]; list *list2 = [[list alloc] initwithtitle:@ "My Phone" groups:l2]; self.datalist = [nsmutablearray arraywithObjects:list2, list1, nil];} @end
========================================
Data Model Implementation
Friend data Object @interface friend : nsobject@property (Nonatomic, retain) NSString *image; // Avatar @property (nonatomic, retain) NSString *name; // nickname @property (nonatomic, retain) NSString *state; // Talk about @end// Group Object @interface group : nsobject@property (nonatomic, assign) BOOL isDelop; // Do you want to expand @property (Nonatomic, retain) NSString *title; // Group name @property (nonatomic, retain) NSString *detail; // Online Status @property (nonatomic, retain) nsmutablearray *friends; // Friends List @end// entire QQ list @interface list : nsobject@property (Nonatomic, retAin) NSString *title; // List name @property (nonatomic, retain) NSMutableArray *groups; // Group @end in List
The method of constructing the data model is not affixed, there is only one construction method for each class
IOS QQ List Effect implementation