用json方法解析本地数据,并显示在tableView上面
2014-09-18 08:56
661 查看
效果图 图片是三张星星图片,1是全星,2是半星,3是空星
类的文件
AppDelegate.m
MainViewController.m显示评分星星的代码可优化,可对传过来的星星数值然后进行个数的判断
JSONParser.h
TableViewCell.m
movie.h
类的文件
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; @endJSONParser.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; @endmovie.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
相关文章推荐
- JSON解析及省市区在tableView上的显示
- Tableview 更多数据的显示方法
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 从接口读取数据,Json解析后显示在RecyclerView上
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 转:Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- TabLayout下ViewPager显示Json解析数据
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 解析json对象,使用的类库jsonkit,但解析完成后,再次使用解析的数据时会导致tableview崩溃
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- Json--Android中数据文件解析(Json解析--从服务器端获取数据并且解析,显示在客户端上面)
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)