用json方法解析本地数据,并显示在tableView上面
效果图 图片是三张星星图片,1是全星,2是半星,3是空星
类的文件
AppDelegate.m
#import"AppDelegate.h"#import"MainViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];MainViewController*mainVC=[[MainViewControlleralloc]init];UINavigationController*naviVC=[[UINavigationControlleralloc]initWithRootViewController:mainVC];self.window.rootViewController=naviVC;[naviVCrelease];[mainVCrelease];[self.windowmakeKeyAndVisible];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
MainViewController.m
显示评分星星的代码可优化,可对传过来的星星数值然后进行个数的判断
#import"MainViewController.h"#import"JSONParser.h"#import"movie.h"#import"TableViewCell.h"@interfaceMainViewController()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)UITableView*tableView;@property(nonatomic,copy)NSMutableArray*arraymovie;@end@implementationMainViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.//开始执行解析的JSON//同时进行block调用JSONParser*json=[[JSONParseralloc]init];self.arraymovie=[NSMutableArrayarray];NSMutableArray*(^passblock)(NSMutableArray*array)=^(NSMutableArray*array){self.arraymovie=array;returnarray;};json.block=passblock;[jsonstartJSONParse];[jsonrelease];self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStylePlain];self.tableView.dataSource=self;self.tableView.delegate=self;[self.viewaddSubview:self.tableView];[_tableViewrelease];}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.arraymovie.count;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*str=@"aaa";TableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:str];if(cell==nil){cell=[[[TableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]autorelease];}movie*mov=[self.arraymovieobjectAtIndex:indexPath.row];cell.label1.text=mov.title;cell.label2.text=mov.pubdate;cell.label3.text=mov.original_title;if([mov.starsisEqualToString:@"00"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"05"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"2.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"10"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"15"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"2.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"20"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"25"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"2.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"30"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"35"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"2.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"40"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"3.png"];}if([mov.starsisEqualToString:@"45"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"2.png"];}if([mov.starsisEqualToString:@"50"]){cell.p_w_picpathView1.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView2.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView3.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView4.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];cell.p_w_picpathView5.p_w_picpath=[UIImagep_w_picpathNamed:@"1.png"];}returncell;}-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{return60;}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
JSONParser.h
#import<Foundation/Foundation.h>@interfaceJSONParser:NSObject@property(nonatomic,copy)NSMutableArray*(^block)(NSMutableArray*array);@property(nonatomic,retain)NSMutableArray*array1;-(void)startJSONParse;@end
JSONParser.m
#import"JSONParser.h"#import"movie.h"@implementationJSONParser-(void)startJSONParse{//系统提供JSON解析方法//1.获取文件路径NSString*strPath=[[NSBundlemainBundle]pathForResource:@"DoubanMovie"ofType:@"txt"];//2.通过路径把文件转换成NDSata类型NSData*data=[NSDatadataWithContentsOfFile:strPath];NSError*error=nil;NSDictionary*dictionary=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:&error];NSMutableArray*array=[[NSMutableArrayalloc]init];array=[dictionaryobjectForKey:@"entries"];//NSLog(@"%@",dictionary);self.array1=[NSMutableArrayarray];for(NSDictionary*dicinarray){//新建一个movie的对象movie*mov=[[moviealloc]init];mov.title=[dicobjectForKey:@"title"];mov.pubdate=[dicobjectForKey:@"pubdate"];mov.original_title=[dicobjectForKey:@"original_title"];mov.stars=[dicobjectForKey:@"stars"];NSLog(@"%@",mov.stars);//把要用的对象都添加到array1中[self.array1addObject:mov];//NSLog(@"%@",self.array1);[movrelease];}//把array1传到前面使用self.block(self.array1);}@end
TableViewCell.m
#import"TableViewCell.h"@implementationTableViewCell-(id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier{self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier];if(self){//Initializationcodeself.label1=[[UILabelalloc]init];self.label1.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.label1];[_label1release];self.label2=[[UILabelalloc]init];self.label2.backgroundColor=[UIColorcyanColor];[self.contentViewaddSubview:self.label2];[_label2release];self.label3=[[UILabelalloc]init];self.label2.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.label3];[_label3release];self.p_w_picpathView1=[[UIImageViewalloc]init];self.p_w_picpathView1.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.p_w_picpathView1];[_p_w_picpathView1release];self.p_w_picpathView2=[[UIImageViewalloc]init];self.p_w_picpathView2.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.p_w_picpathView2];[_p_w_picpathView2release];self.p_w_picpathView3=[[UIImageViewalloc]init];self.p_w_picpathView3.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.p_w_picpathView3];[_p_w_picpathView3release];self.p_w_picpathView4=[[UIImageViewalloc]init];self.p_w_picpathView4.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.p_w_picpathView4];[_p_w_picpathView4release];self.p_w_picpathView5=[[UIImageViewalloc]init];self.p_w_picpathView5.backgroundColor=[UIColorwhiteColor];[self.contentViewaddSubview:self.p_w_picpathView5];[_p_w_picpathView5release];}returnself;}-(void)layoutSubviews{self.label1.frame=CGRectMake(10,0,150,30);self.label2.frame=CGRectMake(10,30,120,30);self.label3.frame=CGRectMake(180,0,180,30);self.p_w_picpathView1.frame=CGRectMake(180,30,25,25);self.p_w_picpathView2.frame=CGRectMake(205,30,25,25);self.p_w_picpathView3.frame=CGRectMake(230,30,25,25);self.p_w_picpathView4.frame=CGRectMake(255,30,25,25);self.p_w_picpathView5.frame=CGRectMake(280,30,25,25);}-(void)dealloc{[_label1release];[_label2release];[_label3release];[_p_w_picpathView1release];[_p_w_picpathView2release];[_p_w_picpathView3release];[_p_w_picpathView4release];[_p_w_picpathView5release];[superdealloc];}-(void)awakeFromNib{//Initializationcode}-(void)setSelected:(BOOL)selectedanimated:(BOOL)animated{[supersetSelected:selectedanimated:animated];//Configuretheviewfortheselectedstate}@end
movie.h
#import<Foundation/Foundation.h>@interfacemovie:NSObject@property(nonatomic,retain)NSString*title;@property(nonatomic,retain)NSString*pubdate;@property(nonatomic,retain)NSString*original_title;@property(nonatomic,copy)NSString*stars;@end
movie.m
#import"movie.h"@implementationmovie-(void)dealloc{[_titlerelease];[_pubdaterelease];[_original_titlerelease];[superdealloc];}-(NSString*)description{return[NSStringstringWithFormat:@"title:%@pubdate:%@original_title:%@stars:%@",self.title,self.pubdate,self.original_title,self.stars];}@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。