用json方法解析本地数据,并显示在tableView上面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用json方法解析本地数据,并显示在tableView上面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含12844字,纯文字阅读大概需要19分钟。
内容图文
![用json方法解析本地数据,并显示在tableView上面](/upload/InfoBanner/zyjiaocheng/1093/fd0fece2a396453cac7f28c910feb31d.jpg)
效果图 图片是三张星星图片,1是全星,2是半星,3是空星
类的文件
AppDelegate.m
#import "AppDelegate.h" #import "MainViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; MainViewController *mainVC = [[MainViewController alloc] init]; UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.rootViewController = naviVC; [naviVC release]; [mainVC release]; [self.window makeKeyAndVisible]; [_window release ]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
MainViewController.m
显示评分星星的代码可优化,可对传过来的星星数值然后进行个数的判断
#import "MainViewController.h" #import "JSONParser.h" #import "movie.h" #import "TableViewCell.h" @interface MainViewController () <UITableViewDataSource, UITableViewDelegate> @property (nonatomic , retain)UITableView *tableView; @property (nonatomic , copy)NSMutableArray *arraymovie; @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //开始执行解析的JSON //同时进行block调用 JSONParser *json = [[JSONParser alloc] init]; self.arraymovie = [NSMutableArray array]; NSMutableArray *(^passblock)(NSMutableArray *array) = ^(NSMutableArray *array){ self.arraymovie = array; return array; }; json.block = passblock; [json startJSONParse]; [json release]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; [_tableView release]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arraymovie.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str = @"aaa"; TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease]; } movie *mov = [self.arraymovie objectAtIndex:indexPath.row]; cell.label1.text = mov.title; cell.label2.text = mov.pubdate; cell.label3.text = mov.original_title; if ([mov.stars isEqualToString:@"00"] ) { cell.imageView1.image = [UIImage imageNamed:@"3.png"]; cell.imageView2.image = [UIImage imageNamed:@"3.png"]; cell.imageView3.image = [UIImage imageNamed:@"3.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"05"] ) { cell.imageView1.image = [UIImage imageNamed:@"2.png"]; cell.imageView2.image = [UIImage imageNamed:@"3.png"]; cell.imageView3.image = [UIImage imageNamed:@"3.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"10"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"3.png"]; cell.imageView3.image = [UIImage imageNamed:@"3.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"15"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"2.png"]; cell.imageView3.image = [UIImage imageNamed:@"3.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"20"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"3.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"25"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"2.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"30"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"1.png"]; cell.imageView4.image = [UIImage imageNamed:@"3.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"35"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"1.png"]; cell.imageView4.image = [UIImage imageNamed:@"2.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"40"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"1.png"]; cell.imageView4.image = [UIImage imageNamed:@"1.png"]; cell.imageView5.image = [UIImage imageNamed:@"3.png"]; } if ([mov.stars isEqualToString:@"45"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"1.png"]; cell.imageView4.image = [UIImage imageNamed:@"1.png"]; cell.imageView5.image = [UIImage imageNamed:@"2.png"]; } if ([mov.stars isEqualToString:@"50"] ) { cell.imageView1.image = [UIImage imageNamed:@"1.png"]; cell.imageView2.image = [UIImage imageNamed:@"1.png"]; cell.imageView3.image = [UIImage imageNamed:@"1.png"]; cell.imageView4.image = [UIImage imageNamed:@"1.png"]; cell.imageView5.image = [UIImage imageNamed:@"1.png"]; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
JSONParser.h
#import <Foundation/Foundation.h> @interface JSONParser : NSObject @property (nonatomic , copy)NSMutableArray *(^block)(NSMutableArray *array); @property (nonatomic , retain)NSMutableArray *array1; - (void)startJSONParse; @end
JSONParser.m
#import "JSONParser.h" #import "movie.h" @implementation JSONParser - (void)startJSONParse { //系统提供JSON解析方法 //1.获取文件路径 NSString *strPath = [[NSBundle mainBundle] pathForResource:@"DoubanMovie" ofType:@"txt"]; //2.通过路径把文件转换成NDSata类型 NSData *data = [NSData dataWithContentsOfFile:strPath]; NSError *error = nil; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSMutableArray *array = [[NSMutableArray alloc] init]; array = [dictionary objectForKey:@"entries"]; // NSLog(@"%@", dictionary); self.array1 = [NSMutableArray array]; for (NSDictionary *dic in array) { //新建一个movie的对象 movie *mov = [[movie alloc] init]; mov.title = [dic objectForKey:@"title"]; mov.pubdate = [dic objectForKey:@"pubdate"]; mov.original_title = [dic objectForKey:@"original_title"]; mov.stars = [dic objectForKey:@"stars"]; NSLog(@"%@",mov.stars); //把要用的对象都添加到array1中 [self.array1 addObject:mov]; // NSLog(@"%@",self.array1); [mov release]; } //把array1传到前面使用 self.block(self.array1); } @end
TableViewCell.m
#import "TableViewCell.h" @implementation TableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.label1 = [[UILabel alloc] init]; self.label1.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.label1]; [_label1 release]; self.label2 = [[UILabel alloc] init]; self.label2.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:self.label2]; [_label2 release]; self.label3 =[[UILabel alloc] init]; self.label2.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.label3]; [_label3 release]; self.imageView1 = [[UIImageView alloc] init]; self.imageView1.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imageView1]; [_imageView1 release]; self.imageView2 = [[UIImageView alloc] init]; self.imageView2.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imageView2]; [_imageView2 release]; self.imageView3 = [[UIImageView alloc] init]; self.imageView3.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imageView3]; [_imageView3 release]; self.imageView4 = [[UIImageView alloc] init]; self.imageView4.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imageView4]; [_imageView4 release]; self.imageView5 = [[UIImageView alloc] init]; self.imageView5.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.imageView5]; [_imageView5 release]; } return self; } - (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.imageView1.frame = CGRectMake(180, 30, 25, 25); self.imageView2.frame = CGRectMake(205, 30, 25, 25); self.imageView3.frame = CGRectMake(230, 30, 25, 25); self.imageView4.frame = CGRectMake(255, 30, 25, 25); self.imageView5.frame = CGRectMake(280, 30, 25, 25); } - (void)dealloc { [_label1 release]; [_label2 release]; [_label3 release]; [_imageView1 release]; [_imageView2 release]; [_imageView3 release]; [_imageView4 release]; [_imageView5 release]; [super dealloc]; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
movie.h
#import <Foundation/Foundation.h> @interface movie : 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" @implementation movie - (void)dealloc { [_title release]; [_pubdate release]; [_original_title release]; [super dealloc]; } - (NSString *)description { return [NSString stringWithFormat:@"title:%@ pubdate:%@ original_title:%@ stars:%@", self.title, self.pubdate, self.original_title, self.stars]; } @end
本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1554585
原文:http://liuyafang.blog.51cto.com/8837978/1554585
内容总结
以上是互联网集市为您收集整理的用json方法解析本地数据,并显示在tableView上面全部内容,希望文章能够帮你解决用json方法解析本地数据,并显示在tableView上面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。