2012-10-02 14 views
0

Mon application utilise une table qui est alimentée par un flux XML dynamique. J'utilise aussi le pull pour actualiser le code écrit par Grant Paul pour recharger la table avec du nouveau contenu, s'il y en a. Troisièmement, j'utilise l'accessibilité d'Apple pour déterminer la connectivité Internet lors du rechargement de la table. Mon problème est ceci: Si j'ouvre l'application AVEC une connexion internet, alors que l'application est en cours d'exécution, fermée de ma connexion internet, je tire alors pour rafraichir l'application et elle se bloque. Il se passe quelque chose lorsque l'application bascule entre Accessible et NonRecherche. Voici mon code. L'application échoue avec un Thread 6: signal SIGABRT sur cette ligne: TFHppleElement * element = [elements objectAtIndex: 0];Problème d'actualisation de tableview iOS

Voici le code d'erreur:

2012-10-02 14:00:03.715 FireCom[2229:1517] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x2d63012 0x21a0e7e 0x2d050b4 0x603f 0xd810d5 0xd81034 0x96213557 0x961fdcee) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

Voici le code joignabilité:

- (void)loadCallList 
{ 
Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; 
NetworkStatus netStatus = [reach currentReachabilityStatus]; 

switch (netStatus) 
{ 
    case NotReachable: 
    { 
     NSLog(@"Access Not Available"); 

     [pull finishedLoading]; 
     [self displayInternetMessage]; 

     break; 
    } 

    case ReachableViaWWAN: 
    { 
     NSLog(@"Reachable WWAN"); 

     // Parse HTML for random ID and create XML link 

     NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:theURL]; 
     xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
     NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
     TFHppleElement *element = [elements objectAtIndex:0]; 
     TFHppleElement *child = [element.children objectAtIndex:0]; 
     NSString *idValue = [child content]; 

     NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"]; 
     NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_"; 
     NSString *finalurl = [url stringByAppendingString:idwithxml]; 

     xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl]; 

     if (xmlParser.calls.count == 0) 
     { 
      self.headerHeight = 0.0; 
      UIAlertView *noCalls = [[UIAlertView alloc] initWithTitle:@"No Current Calls" 
                   message:@"There are no current 9-1-1 calls in Clackamas or Washington County." 
                  delegate:self 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

      [noCalls show]; 
      [noCalls release]; 
     } 
     else 
     { 
      self.headerHeight = 45.0; 
     } 

     [callsTableView reloadData]; 
     [pull finishedLoading]; 

     break; 
    } 
    case ReachableViaWiFi: 
    { 
     NSLog(@"Reachable WiFi"); 

     // Parse HTML for random ID and create XML link 

     NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:theURL]; 
     xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
     NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
     TFHppleElement *element = [elements objectAtIndex:0]; 
     TFHppleElement *child = [element.children objectAtIndex:0]; 
     NSString *idValue = [child content]; 

     NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"]; 
     NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_"; 
     NSString *finalurl = [url stringByAppendingString:idwithxml]; 

     xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl]; 

     if (xmlParser.calls.count == 0) 
     { 
      self.headerHeight = 0.0; 

      UIAlertView *noCalls = [[UIAlertView alloc] initWithTitle:@"No Current Calls" 
                   message:@"There are no current 9-1-1 calls in Clackamas or Washington County." 
                  delegate:self 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

      [noCalls show]; 
      [noCalls release]; 
     } 
     else 
     { 
      self.headerHeight = 45.0; 
     } 

     [callsTableView reloadData]; 
     [pull finishedLoading]; 

     break; 
    } 
} 
} 
+3

Votre code suppose que votre tableau 'elements' aura des données. Vous devez d'abord vérifier si nil ou count == 0 et ajouter une logique pour gérer un scénario «sans données». – Jeremy

+0

le tableau d'éléments aura TOUJOURS des données. ça ne sera jamais vide. –

+0

Et si 'xpathParser' ne trouve aucun élément avec un identifiant de' hidXMLID'? Je pense que la réponse réside dans les données. Vous ne récupérez pas ce que vous pensez être. Vos problèmes sont plutôt simples. Les éléments n'ont rien dedans, ainsi vous obtenez l'erreur. – Jeremy

Répondre

1

Je pense que je vois votre problème.

Vous ne pouvez pas supposer elements a un nombre d'au moins 1.


Mise à jour

Peut-être essayer quelque chose comme

NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
if (elements.count < 1) { 
    // Any needed cleanup 
    break; 
} 

TFHppleElement *element = [elements objectAtIndex:0]; 
TFHppleElement *child = [element.children objectAtIndex:0]; 

comme rapide sortie.

+0

Comme je l'ai dit plus haut, le tableau des éléments aura toujours des données! il analyse un fichier HTML pour rechercher un ID, puis crée une URL que l'analyseur XML peut utiliser pour analyser. –