Group presentation of complex models
#import "HMViewController.h"#import "HMCarGroup.h"#import "HMCar.h"@interfaceHmviewcontroller () <UITableViewDataSource>@property (nonatomic, strong) Nsarray*cargroups, @property (nonatomic, strong) UITableView*TableView;@end@implementationHmviewcontroller-(UITableView *) tableview{if(_tableview = =Nil) {_tableview=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; _tableview.datasource=Self ; [Self.view Addsubview:_tableview]; } return_tableview;}-(Nsarray *) cargroups{if(_cargroups = =Nil) {_cargroups=[Hmcargroup cargroups]; } return_cargroups;}- (void) viewdidload{[Super Viewdidload]; //call TableView Add to view[self TableView];}#pragmaMark-Data Source methods//total number of groupings-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSelf.carGroups.count;}//total for each group-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Hmcargroup*group =Self.cargroups[section]; returnGroup.cars.count;}//Cell Cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Reusable Identifiers StaticNSString *id =@"Cell"; //Let table buffers find reusable cellsUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:id]; //If you do not find a reusable cell if(Cell = =Nil) { //Instantiate cellCell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; } //Set cell contents//1> extracting the data modelHmcargroup *group =Self.cargroups[indexpath.section]; Hmcar*car =Group.cars[indexpath.row]; //2> setting up dataCell.imageView.image =[UIImage ImageNamed:car.icon]; Cell.textLabel.text=Car.name; returncell;}//title-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{//Find GroupHmcargroup *group =Self.cargroups[section]; returnGroup.title;}//Right Index list-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//"Content" in an indexed array, independent of grouping//Subscript in an indexed array, corresponding to a grouped subscript//return @[@ "wow", @ "Hello", @ "wow haha", @ "Hello", @ "wow haha", @ "Hello" @ "wow haha" @ "Hello"]; //returns an array of title in Self.cargroup//Nsmutablearray *arraym = [Nsmutablearray array];//For (Hmcargroup *group in self.cargroups) {//[Arraym AddObject:group.title];// }//return Arraym; //KVC is Cocoa's big trick.//ways to indirectly get or modify the properties of an object//using KVC to get a numeric value, if the specified object does not contain KeyPath's "key Name", it automatically enters the object's internal lookup//if the object being evaluated is an array, it also returns an arrayNsarray *array = [self.cargroups valueforkeypath:@"Cars.name"]; NSLog (@"%@", array); return[Self.cargroups Valueforkeypath:@"title"];}@end
iOS Seventh day (5:uitableview car brand, complex model group show, A-Z request list) (2015-08-05-14:03)