We can use the name of the segment method, the segment name will appear in the following sections, which is used instead of role players segmented hair, in the most direct starting point to explain the use of a good segment of UITableView. This example is based on SimpleTableViewController section when the section here can be used as a parent class, create a class named SectionTableViewController, inheritance from SimpleTableViewController, the header file the following statement:
#import "HBSimpleTableViewController.h" @interface HBSectionTableViewController : HBSimpleTableViewController @end
Then it is necessary to make the data source improvements, so that it becomes the role category
- ( void ) initData { // recognize the validity of the data source in SimpleTableView [Super initData]; // end new data source NSMutableArray arrSectionDatasource * = [NSMutableArray the arrayWithCapacity: 0 ]; // record the name of the current section have been used NSMutableArray arrSection = * [NSMutableArray the arrayWithCapacity: 0 ]; // temporarily storing a segment of all the player objects whose name NSMutableArray arrTmp * = [NSMutableArray the arrayWithCapacity: 0 ]; // traverse all players for (* onePlayer HBPlayerInfo in self.datasource) { NSString * Role = onePlayer.role; // if the current player has been doing role only as a section name, the Continue // means that this player has been added to the final data source IF ([arrSection containsObject: role]) { Continue ; } // New role // iterate again players for (* HBPlayerInfo roleplayer in self.datasource) { IF ([rolePlayer.role as isEqualToString: role]) { [arrTmp addObject: roleplayer]; } } // this role is as a segment name, finished [arrSection addObject: role]; // arrTmp contains all meet the current segment name role player objects // added to the final data source [arrSectionDatasource addObject: arrTmp]; // reset arrTmp // wait for a new role of the player objects satisfy all add to the mix arrTmp = [NSMutableArray the arrayWithCapacity: 0 ]; } // reset the data source IF (_datasource) { _datasource = nil ; } _datasource = [[NSArray the alloc] initWithArray: arrSectionDatasource]; }
(1) tells UITableView total divided into paragraphs, before SimpleTableViewController not implement this method, so the default time period, the multi-segment code is as follows:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.datasource.count; }
(2) Tell UITableView section names each segment. Since the role of the contents of each sub-array are the same elements, the first element may take access
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { HBPlayerInfo *onePlay=[((NSArray *)[self.datasource objectAtIndex:section]) objectAtIndex:0]; return onePlay.role; }
Table View "production line" callback function to re-access the new data source, is modified as follows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ((NSArray *)[self.datasource objectAtIndex:section]).count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier=@"sectionTableViewCellId"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //取得每个球员的方法和SimpleTableViewController有所不同 HBPlayerInfo *onePlayer=nil; NSArray *arrSectionPlayer=[self.datasource objectAtIndex:indexPath.section]; if(arrSectionPlayer && arrSectionPlayer.count>indexPath.row) { onePlayer =[arrSectionPlayer objectAtIndex:indexPath.row]; } if (onePlayer) { cell.textLabel.text=onePlayer.name; } return cell; }
Program run effect is as follows:
Reproduced in: https: //www.cnblogs.com/haibosoft/p/3668709.html