您好,登錄后才能下訂單哦!
效果圖 圖片是三張星星圖片,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
顯示評分星星的代碼可優(yōu)化,可對傳過來的星星數(shù)值然后進(jìn)行個(gè)數(shù)的判斷
#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. //開始執(zhí)行解析的JSON //同時(shí)進(jìn)行block調(diào)用 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 { //系統(tǒng)提供JSON解析方法 //1.獲取文件路徑 NSString *strPath = [[NSBundle mainBundle] pathForResource:@"DoubanMovie" ofType:@"txt"]; //2.通過路徑把文件轉(zhuǎn)換成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) { //新建一個(gè)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]; } //把a(bǔ)rray1傳到前面使用 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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。