在iOS中使用TBXML解析XML时如何同步?

时间:2014-03-12 13:27:50

标签: ios objective-c xml uitableview tbxml

我遇到了一个问题:当我在Afnetworking的setCompletionBlockWithSuccess中使用TBXML调用解析器xml时,我的tableview会频繁重载并且无法停止。

...
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

            [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"Success");
                NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];


                NSError * error = nil;
    TBXML *sourceXML = [[TBXML alloc] initWithXMLString:parsexmlinput error:&error];
    if (!error) {
        /** get ROOT element **/
        TBXMLElement *status = sourceXML.rootXMLElement;
        NSString *statusString = [TBXML elementName:status];
        NSLog(@"ROOT: %@", statusString);

        /** get child element **/
        TBXMLElement *listElement = [TBXML childElementNamed:@"FILE_LIST" parentElement:status];

        TBXMLElement *itemElement = [TBXML childElementNamed:@"FILE" parentElement:listElement];


        do
        {
            TBXMLElement *uploadIDElement = [TBXML childElementNamed:@"UPLOAD_ID" parentElement:itemElement];
            [UploadIDArray addObject:[TBXML textForElement:uploadIDElement]];

            TBXMLElement *filenameElement = [TBXML childElementNamed:@"FILENAME" parentElement:itemElement];
            [FileNameArray addObject:[TBXML textForElement:filenameElement]];

        } while ((itemElement = itemElement->nextSibling) != nil);

        //fill in Dictionary
        [Upload_Status_Dic setObject:FileNameArray forKey:@"FileName"];
        [Upload_Status_Dic setObject:UploadIDArray forKey:@"UploadID"];

        NSLog(@"Upload_Status_Dic %@",Upload_Status_Dic);
        // release resources
        NSLog(@"Dictionary is %@",Upload_Status_Dic);
        //update your UI
        AllItemsArray = [Upload_Status_Dic valueForKeyPath:@"FileName"];

        NSLog(@"AllItemsArray is %@",AllItemsArray);

        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];

        _tableView.dataSource = self;
        _tableView.delegate=self;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.view addSubview:_tableView];

}

在代码上方运行时,我的应用程序始终调用UITableView的numberOfRowsInSection。我认为TBXML解析器仍然可以解析XML。我如何等待TBXML解析器完成并将数据加载到UITableView?应该欣赏任何想法。提前谢谢。

0 个答案:

没有答案