Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  Ski
  4. //
  5. //  Created by Administrateur on 15/11/2014.
  6. //  Copyright (c) 2014 Administrateur. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "ViewController2.h"
  11.  
  12. @interface ViewController () {
  13.     NSArray *pays;
  14.     NSArray *pays2;
  15. }
  16. @property (weak, nonatomic) IBOutlet UITableView *tablePays;
  17.  
  18. @end
  19.  
  20. @implementation ViewController
  21.  
  22. - (void)viewDidLoad {
  23.     [super viewDidLoad];
  24.     // Do any additional setup after loading the view, typically from a nib.
  25.    
  26.     pays = @[@"France",@"Mexique",@"Suisse",@"Italie"];
  27.    
  28.     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  29.    
  30.     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/pays.php"];
  31.     NSURLRequest *requete = [NSURLRequest requestWithURL:url];
  32.     [NSURLConnection sendAsynchronousRequest:requete
  33.                                        queue:[NSOperationQueue mainQueue]
  34.                            completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
  35.      {
  36.          pays2 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  37.          NSLog(@"%@", pays2);
  38.          
  39.          [_tablePays reloadData];
  40.          
  41.          [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  42.      }
  43.      ];
  44.  
  45.    
  46. }
  47.  
  48.  
  49.  
  50. - (void)didReceiveMemoryWarning {
  51.     [super didReceiveMemoryWarning];
  52.     // Dispose of any resources that can be recreated.
  53. }
  54.  
  55. #pragma mark UITableViewDataSource
  56. // Les methodes <UITableViewDataSource>
  57. //La classe viewController est configurée comme DataSource delegate de la tableview
  58.  
  59. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  60.     return pays2.count;
  61. }
  62.  
  63. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  64.     //reutilisé une cellule existante?
  65.     UITableViewCell *cellule = [tableView dequeueReusableCellWithIdentifier:@"stock"];
  66.    
  67.     if (!cellule) {//creation d'une nouvelle fenetre avec style et identifiant
  68.         cellule = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"stock"];
  69.     }
  70.    
  71.     //indexPath.row : n° de ligne de tableview
  72.     cellule.textLabel.text = pays2[indexPath.row][@"Pays"];
  73.     cellule.accessoryType = UITableViewCellAccessoryDetailButton;
  74.    
  75.     //image
  76.     cellule.imageView.image =
  77.     [UIImage imageWithData:
  78.      [NSData dataWithContentsOfURL:
  79.       [NSURL URLWithString:
  80.        [NSString stringWithFormat:@"http://127.0.0.1/flags/%@.png", pays2[indexPath.row][@"code"]]]]];
  81.    
  82.     return cellule;
  83. }
  84.  
  85. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  86.    
  87.     NSLog(@"Ligne %ld", indexPath.row);
  88.     [self performSegueWithIdentifier:@"liaison" sender:self];
  89. }
  90.  
  91.  
  92. //prepare la liaison
  93. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  94.     //on recupere la vue de destination
  95.     ViewController2 *view = (ViewController2 *)segue.destinationViewController;
  96.    
  97.     //On regle la propriete @property code_pays de ViewController2
  98.     view.code_pays = @"FR";
  99. }
  100.  
  101. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement