2013-05-09 1 views
2

Tableau:Vous voulez afficher les valeurs de tableau dans le tableau

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 
return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{ 
return [self.detailArray count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
tableView.delegate = self; 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 300.0f, 30.0f)]; 
label.text=[NSString stringWithFormat:@"%@",[self.detailArray objectAtIndex:indexPath.row]]; 
label.numberOfLines = 3; 
label.font = [UIFont fontWithName:@"Helvetica" size:(12.0)]; 
label.lineBreakMode = UILineBreakModeWordWrap; 
label.textAlignment = UITextAlignmentLeft; 
[cell.contentView addSubview:label]; 
[label release]; 
[self.myTableView reloadData]; 

return cell; 

} Je veux stocker les détails (valeur de tableau) dans la tableView .Je utilisé à la fois la méthode numberOfRowsInSection et cellForRowAtIndexPath mais l'affichage de la table null . Comment puis-je afficher?

+0

Vous rechargeait la vue de la table après que les données ont été téléchargées? – Wain

+0

pouvez-vous remplir le tableau 'detail'? –

+0

oui j'ai rechargé mais pas de changement. –

Répondre

1

Faites de la propriété pour le tableau NSMutable dans .h

@property (nonatomic,retain) NSMutableArray *detailArray; 

et synthétiser dans .m

@synthesize detailArray = _detailArray;

et changer cette ligne NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

à

_detailArray=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ; 

puis utilisez _detailArray pour afficher des données de table.

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return [_detailArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// _detailArray objects for date population. 
} 
+0

J'ai donné _detailArray dans la méthode cellForRowAtIndexPath mais il ne montre aucune valeur –

+0

que vous utilisez arc ou non. – Ishu

+0

Je n'utilise pas d'arc. –

0

d'abord vous connecter vérifié votre délégué tableview set auto ...

deuxième chef d'accusation pr detailarray suite à son numberOfRowsInSection peut b zéro

0

1er vérifié youn ligne Déclarez votre tableau dans .h

comme celui-ci

@property(strong,nonatomic)NSMutableArray * name; 

Et intialize int o .m méthode Init()

name=[NSMutableArray alloc]initWithObjects:@"a",@"b", nil]; 

// TableView Méthode délégué à afficher un tableau de données dans Tableview.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
     { 
      return 1; 
     } 

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
     { 
       return name.count; //Give Your Array Name Here. 
     } 

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      static NSString *CellIdentifier = @"Cell"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      } 

      cell.textLabel.text=[NSString stringWithFormat:@"%@",[name objectAtIndex:indexPath.row]]; 



      return cell; 
     } 

Essayez ce code.

+0

rien de changement dans la vue de la table.please m'aider si vous connaissez une autre idée –

+0

êtes-vous essayé ce code.Ceci va fonctionner correctement. – Jitendra

+0

oui j'ai essayé. ma valeur de tableau est comme direction de route. vous voyez mon NSLOG "instruction de HTML" .s'affiche la direction de direction, est-ce que la valeur de route s'ajoute dans la table ou pas? –

1

Votre code:

NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude]; 
NSURL *googleRequestURL=[NSURL URLWithString:url]; 
dispatch_async(kBgQueue, ^{ 
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
    NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

    // NSLog(@"data:%@",someString); 

    NSError* error; 
    NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data    options:kNilOptions error:&error]; 

    NSArray *allkeys = [parsedJson allKeys]; 

    for(int i = 0; i < allkeys.count; i++){ 

     if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){ 
      arr = [parsedJson objectForKey:@"routes"]; 
      dic = [arr objectAtIndex:0]; 
      // NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]); 
      legs = [dic objectForKey:@"legs"]; 
      // NSLog(@"legs array count %d", legs.count); 
      for(int i = 0; i < legs.count; i++){ 
       stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"]; 
       for (int i = 0; i < stepsArr.count; i++) { 
        NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]); 
        NSLog(@"############################"); 
        NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ; 
       } 
      } 

     } 

    }   
}); 

d'abord vous allez à votre fichier .h ->

@property (nonatomic, strong) NSMutableArray * detailsArray; 

Dans.fichier m ->

@synthesize detailsArray; 

remplacer ce code

NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ; 

par cette ->

self.detailsArray = [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]; 


# using table datasource methods 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    return [self.detailsArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

cell.titleLabel.text = [self.detailsArray objectAtIndex: indexPath.row ]; 

return cell; 
} 

}

// Remarque S'il vous plaît définir la méthode de délégué table, si vous écrivez le code signifie par programme tableView.delgate = self;

Utilisez ceci:

self.detaisArray = [[NSMutableArray alloc] init]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude]; 
    NSURL *googleRequestURL=[NSURL URLWithString:url]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
     NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

     // NSLog(@"data:%@",someString); 

     NSError* error; 
     NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data    options:kNilOptions error:&error]; 

     NSArray *allkeys = [parsedJson allKeys]; 

     for(int i = 0; i < allkeys.count; i++){ 

      if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){ 
       NSArray *arr = [parsedJson objectForKey:@"routes"]; 
       NSDictionary *dic = [arr objectAtIndex:0]; 
       // NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]); 
       NSArray *legs = [dic objectForKey:@"legs"]; 
       // NSLog(@"legs array count %d", legs.count); 
       for(int i = 0; i < legs.count; i++){ 
        NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"]; 
        for (int i = 0; i < stepsArr.count; i++) { 
         NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]); 
         NSLog(@"############################"); 
         [self.detaisArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ]; 
         if(i == legs.count-1){ 
          self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 400) style:UITableViewStylePlain]; 
          self.myTableView.delegate = self; 
          self.myTableView.dataSource = self; 
          [self.view addSubview:self.myTableView]; 
         } 
        } 
       } 

      } 

     }   
    }); 
+0

désolé de le dire. sa valeur null reste affichée uniquement. –

+0

vous voyez mon NSLOG pour "instruction HTML" .s'affiche direction route, est-ce que la valeur de route est ajoutée dans la table ou non? –

+0

Oui, je veux voir votre NSLOG, et pouvez-vous me dire quel type de valeur que vous souhaitez imprimer. Parce que le service renvoie les valeurs Dictionary et plusieurs paramètres. les valeurs de paramètre que vous voulez afficher. –

Questions connexes