yjcar.h//03-tableview-Auto Brand////Created by Jacky-mac on 15-6-24.//Copyright (c) 2015 www.train.com. All rights reserved.//dictionary to model #import <Foundation/Foundation.h> @interface yjcar:nsobject@property (nonatomic, copy) NSString *icon; @property (nonatomic,copy) nsstring *name;-(instancetype) initwithdict: (Nsdictionary *) dict;+ ( Instancetype) Carwithdict: (Nsdictionary *) dict;+ (Nsarray *) Carswitharray: (Nsarray *) array; @end////hmcar.m//05-Automotive Brands Created by Apple on 14-8-19.//Copyright (c) 2014 itcast. All rights reserved.//#import ' HMCar.h ' @implementation hmcar-(instancetype) initwithdict: (Nsdictionary *) dict{self = [ Super Init]; if (self) {[self setvaluesforkeyswithdictionary:dict]; } return self;} + (Instancetype) carwithdict: (nsdictionary *) dict{return [[Self alloc] initwithdict:dict];} + (Nsarray *) Carswitharray: (Nsarray *) array{nsmutablearray *arraym = [Nsmutablearray array]; For (nsdictionary *dict in array) {[Arraym Addobject:[selF Carwithdict:dict]]; } return Arraym;} -(NSString *) description{return [NSString stringwithformat:@ "<%@:%p> {name:%@, Icon:%@}", Self.class, Self,s Elf.name, Self.icon];} @end////hmcargroup.h//05-car brand////Created by Apple on 14-8-19.//Copyright (c) 2014 itcast. All rights reserved.//#import <Foundation/Foundation.h> @interface hmcargroup:nsobject/** Initials */@property ( nonatomic, copy) NSString *title;/** car array, storing HMCAR model data */@property (nonatomic, Strong) Nsarray *cars;-(instancetype) Initwithdict: (Nsdictionary *) dict;+ (instancetype) cargroupwithdict: (Nsdictionary *) dict;+ (NSArray *) carGroups; @end hmcargroup.m//05-car brand////Created by Apple on 14-8-19.//Copyright (c) 2014 itcast. All rights reserved.//#import "HMCarGroup.h" #import "HMCar.h" @implementation hmcargroup-(instancetype) initwithdict :(nsdictionary *) dict{self = [super init]; if (self) {//[self setvaluesforkeyswithdictionary:dict]; [Self setvalue:dict[@ "title"] forkey:@ "title"]; dict[@ "Cars" is an array of dictionaries//You want to convert an array of dictionaries into an array of hmcar models//[self setvalue:dict[@ "cars"] forkey:@ "cars"]; Self.cars = [Hmcar carswitharray:dict[@ "Cars"]; } return self;} + (Instancetype) cargroupwithdict: (nsdictionary *) dict{return [[Self alloc] initwithdict:dict];} + (Nsarray *) cargroups{nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathForResource:@ "Cars _total.plist "Oftype:nil]"; Nsmutablearray *arraym = [Nsmutablearray array]; For (nsdictionary *dict in array) {[Arraym addobject:[self cargroupwithdict:dict]]; } return Arraym;} -(NSString *) description{return [NSString stringwithformat:@ "<%@:%p> {title:%@, Cars:%@}", Self.class, Self, Self.title, Self.cars];} hmviewcontroller.m//05-car brand////Created by Apple on 14-8-19.//Copyright (c) 2014 itcast. All rights reserved.//#import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar.h" @interfaceHmviewcontroller () <UITableViewDataSource> @property (nonatomic, strong) Nsarray *cargroups; @property ( Nonatomic, strong) UITableView *tableview; @end @implementation hmviewcontroller-(UITableView *) tableView{if (_tableVi EW = = 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];} #pragma mark-Data source method//Total number of groupings-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return self.carGroups.co UNT;} Total number of each group-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Hmcargroup *group = Self.cargroups[section]; return group.cars.count;} Cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Reusable identifier static NSString *id = @ "Cell"; Let the table buffer find reusable cell uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:id]; If you do not find a reusable cell if (cell = = nil) {//Instantiate cell cell = [[UITableViewCell alloc] Initwithstyle:uitableviewc Ellstyledefault Reuseidentifier:id]; }//Set cell contents//1> out data model hmcargroup *group = self.cargroups[indexpath.section]; Hmcar *car = Group.cars[indexpath.row]; 2> setting Data Cell.imageView.image = [UIImage ImageNamed:car.icon]; Cell.textLabel.text = Car.name; return cell;} Title-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{//Find group Hmcargr OUP *group = self.cargroups[section]; return group.title;} Right index list-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//Index Array "Contents", followed by group Independent//index array subscript, corresponding to the sub- Group 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.carg roups) {//[Arraym addobject:group.title];//}//return arraym; KVC is Cocoa's big trick//used to indirectly acquire or modify the properties of an object.//Use KVC when retrieving a value, if the specified object does not contain a KeyPath "key name", it automatically enters the object's internal lookup//If the value of the object is an array, also returns an array Nsarray *array = [self.cargroups valueforkeypath:@ "Cars.name"]; NSLog (@ "%@", array); return [self.cargroups valueforkeypath:@ "title"];} @end
iOS Learning-tableView (auto brand)