温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用json方法解析本地数据,并显示在tableView上面

发布时间:2020-07-01 21:48:02 来源:网络 阅读:1210 作者:Im刘亚芳 栏目:开发技术

效果图  图片是三张星星图片,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.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"05"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"2.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"10"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"15"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"2.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"20"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"25"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"2.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"30"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"35"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"2.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"40"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"3.png"];
    }
    
    if ([mov.stars isEqualToString:@"45"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"2.png"];
    }
    
    if ([mov.stars isEqualToString:@"50"] ) {
        cell.p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView4.p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];
        cell.p_w_picpathView5.p_w_picpath = [UIImage p_w_picpathNamed:@"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.p_w_picpathView1 = [[UIImageView alloc] init];
        self.p_w_picpathView1.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.p_w_picpathView1];
        [_p_w_picpathView1 release];
        
        self.p_w_picpathView2 = [[UIImageView alloc] init];
        self.p_w_picpathView2.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.p_w_picpathView2];
        [_p_w_picpathView2 release];
        
        self.p_w_picpathView3 = [[UIImageView alloc] init];
        self.p_w_picpathView3.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.p_w_picpathView3];
        [_p_w_picpathView3 release];
        
        self.p_w_picpathView4 = [[UIImageView alloc] init];
        self.p_w_picpathView4.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.p_w_picpathView4];
        [_p_w_picpathView4 release];
        
        self.p_w_picpathView5 = [[UIImageView alloc] init];
        self.p_w_picpathView5.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.p_w_picpathView5];
        [_p_w_picpathView5 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.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
{
    [_label1 release];
    [_label2 release];
    [_label3 release];
    [_p_w_picpathView1 release];
    [_p_w_picpathView2 release];
    [_p_w_picpathView3 release];
    [_p_w_picpathView4 release];
    [_p_w_picpathView5 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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI