2014-11-14 15 views
8

Sto analizzando un dato json e in quello del campo c'è un array se ha un contenuto. Ma se non ha contenuto è semplicemente una stringa "Nessun risultato". Sto usando il seguente codice di analizzarlo:Cell per riga a indexpath chiamato solo una volta

-(void)viewWillAppear:(BOOL)animated 


{ 
[super viewWillAppear:YES]; 
//URL is called. 
if ([status isEqualToString:@"success"]) 

{ 
       if ([[[json objectForKey:@"Items"] objectForKey:@"item_list"] isKindOfClass:[NSString class]]) 



       { 
        _Label.text = [NSString stringWithFormat:@"0"]; 
        ItemsArray = [[NSMutableArray alloc]init]; 
       } 
       else 
       { 
        ItemsArray = [[json objectForKey:@"Items"] objectForKey:@"item_list"]; 
        NSLog(@"%d",[ItemsArray count]); 
        float Amount = 0; 
        NSLog(@"%d",[ItemsArray count]); 
        if ([ItemsArray count]>0) 
        { 
         for (int i=0; i<[ItemsArray count]; i++) 
         { 
          NSMutableDictionary * temp3 = [ItemsArray objectAtIndex: i]; 
          NSString * x = [temp3 objectForKey:@"final"]; 
          Amount = Amount + [x floatValue]; 
         } 
         Label.text = [NSString stringWithFormat:@"%.2f",BidAmount]; 
        } 
        else 
        { 
         Label.text = [NSString stringWithFormat:@"0"]; 
        } 
       } 



       [TableViewOutlet reloadData]; 

}

Questo funziona bene.

Quando il campo restituisce stringa, non ho problemi. Ma quando il campo è un array, sono bloccato. Dopo aver eseguito il progetto per la prima volta funziona bene anche se il numero di array è superiore a 0.Ma dalla seconda volta in poi il metodo Cellforrowatindexpath non viene chiamato anche se il numero di array è maggiore di 0 .... Questi sono i miei codici :

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

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSLog(@"%d",[ItemsArray count]); 
return [ItemsArray count]; 
} 

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

{ 
static NSString *CellIdentifier = @"TableViewCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

if (cell==nil) 
{ 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.025]; 
NSDictionary *temp = [ItemsArray objectAtIndex:indexPath.row]; 

UILabel *ItemName = (UILabel *)[cell viewWithTag:1000]; 
ItemName.text = [temp objectForKey:@"title"]; 

UILabel *Cost = (UILabel *)[cell viewWithTag:1001]; 
Cost.text = [temp objectForKey:@"final"]; 

return cell; 

} 

qualcuno per favore mi aiuti

+0

Verificare il conteggio del 'ItemsArray' –

+0

L'array ha il contenuto .. –

risposta

1

credo che il problema è che non hai mantenuto la tua ItemArray.

NSArray* array = [[json objectForKey:@"Items"] objectForKey:@"item_list"]; 
ItemsArray = [NSMutableArray alloc]]initWithArray:array]; 

Utilizzare questo snippet di codice. Spero che funzionerà.

+0

Di voi ragazzi, ma scusa non funziona –

+0

Scusate ragazzi ,,,,, ha funzionato quando ho combinato entrambe le linee.Grazie molto –

Problemi correlati