我的应用程序使用由动态 xml 提要填充的表。我还使用 Grant Paul 编写的拉动刷新代码来用新内容重新加载表(如果有的话)。第三,我在重新加载表时使用 Apple 的 Reachability 来确定互联网连接。我的问题是这样的;如果我通过互联网连接打开应用程序,那么当应用程序运行时,关闭我的互联网连接,然后我拉刷新应用程序,它就会崩溃。当应用程序在 Reachable 和 NotReachable 之间切换时,会发生某些情况。下面是我的代码。应用程序失败,并在此行显示 Thread 6: signal SIGABRT:TFHppleElement *element = [elements objectAtIndex:0];
错误代码如下:
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)
这是可达性代码:
- (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;
}
}
}
最佳答案
我想我看到了你的问题。
NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];
TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];
您不能假设 elements
的计数至少为 1。
更新
也许可以尝试类似的事情
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];
作为快速出路。
关于iOS 表格 View 刷新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12698294/