#import"RootViewController.h"#import"Htohero.h"@interface Rootviewcontroller ()<UITableViewDataSource>@property (nonatomic, retain) Nsarray*apps, @end @implementation Rootviewcontroller- (void) viewdidload {[Super viewdidload]; //Create UITableViewUITableView *tableview =[[UITableView alloc] Initwithframe:[uiscreen mainscreen].bounds Style:uitableviewstyleplain]; //Configuration Properties//UITableView proxy method for specifying a data source for TableView to be the implementation of a UITableView data source//let control become the TableView data sourceTableview.datasource =Self ; //Add top View[Self.view Addsubview:tableview]; //Release[TableView release];}//there are several parts-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;}//there are several lines in each section-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.apps.count;}//what is displayed in each row-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//cell to implement reuse//Define a flag StaticNSString *id =@"Hero"; //1. Find the cell that can be recycled through the cache pool//dequeue: Dequeue (Find)UITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:id]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; } //Get Object ModelHtohero *hthero =Self.apps[indexpath.row]; Cell.imageView.image=Hthero.heroimage; Cell.textLabel.text=Hthero.heroname; Cell.detailTextLabel.text=Hthero.herodis; returncell;}//import data into the model and then import into the set-(Nsarray *) apps{//This is the implementation of the load of several sentences, the first time to call this method when loading all the data//when this method is called for the second time, it is not loaded//Use lazy loading if(_apps = =Nil) { //1. Read the full path of the Heros.plist fileNSString *path = [[NSBundle mainbundle] Pathforresource:@"heros.plist"Oftype:nil]; //2. Load the array and read out the data in the plistNsarray *dictarray =[Nsarray Arraywithcontentsoffile:path]; //3. Convert a dictionary from Dicarray to a model object and put it in a new array//traversing the Dictarray array//4. Create a mutable array and save the model in a mutable arrayNsmutablearray *dicarray =[Nsmutablearray array]; for(Nsdictionary *dicinchDictarray) { //Initializing Model ObjectsHtohero *hero =[Htohero Herowithdic:dic]; [Dicarray Addobject:hero]; } //5. Assigning ValuesSelf.apps =Dicarray; } return_apps; }- (void) dealloc{[_apps release]; [Super Dealloc];} @end
Htohero.h
#import <Foundation/Foundation.h><UIKit/UIKit.h>* * * Heroimage; // Custom Initialization + (Instancetype) Herowithdic: (Nsdictionary *) dic; -(Instancetype) Initwithdic: (Nsdictionary *) dic; @end
Htohero.m
#import"Htohero.h"@implementation Htohero+ (Instancetype) Herowithdic: (Nsdictionary *) dic{return[Self alloc] initwithdic:dic];}-(Instancetype) Initwithdic: (Nsdictionary *) dic{if(self =[Super Init]) { //KVC Dictionary to model, this is not possible because the data type in the object differs from the data type in DIC//KVC string Names by string name//[self setvaluesforkeyswithdictionary:dic];Self.heroimage = [UIImage imagenamed:dic[@"icon"]]; Self.herodis= dic[@"Intro"]; Self.heroname= dic[@"name"]; } returnSelf ;}- (void) dealloc{[_herodis release]; [_heroname release]; [_heroimage release]; [Super Dealloc];} @end
iOS uitableview--(read data from plist to model, lazy loading, tableview data loading)