iOS 表格 View 刷新问题

标签 ios xml crash reachability sigabrt

我的应用程序使用由动态 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/

相关文章:

c# - 格式错误的引用元素签署账单的 xml 文件

ios - PKInAppPaymentService 连接上的 ApplePay iOS 错误 - com.apple.passd.in-app-payment - 连接到远程警报 View 服务失败

ios - 以编程方式(使用 SnapKit)构建 UI 时如何正确使用大小类?

iphone - 向 APNS 批量发送消息,如果其中一些出现错误怎么办?

c++ - QDomDocument 不会插入 QDomElement

visual-c++ - 当消耗数GB的内存时,vc++ 64位程序在新的运算符上崩溃

ios - Swift 2 的 Xcode 7.0 更新问题

python - jenkins 通过请求发布作业 xml 失败并出现异常

python-3.x - 即使在 chromedriver 之后,Chrome 也会随机崩溃

java - 应用程序不断崩溃