dispatch_queue_t和TableView

时间:2013-11-07 16:31:34

标签: ios objective-c grand-central-dispatch

我是Objective-C语言中的noobie,我有一点问题。 事实上,我有2个TableView,当我从一个到另一个时,我从互联网解析一些XML。解析效果很好,但我想在这两个视图之间添加一个UIActivityIndi​​catorView来告诉用户正在加载某些东西。 所以,为此,我想在另一个线程中进行解析并在主线程中显示UIActivityIndi​​catorView。所以这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIActivityIndicatorView *activityIndicator;
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.center = self.view.center;
    [self.view addSubview: activityIndicator];
    activityIndicator.startAnimating;

    dispatch_queue_t queue = dispatch_get_global_queue(0,0);

    dispatch_async(queue, ^{
        NSError *error = nil;
        //  we will put parsed data in an a array
        titles = [[NSMutableArray alloc] init];
        urls = [[NSMutableArray alloc] init];

        CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:_emissionSelectionnee] options:0 error:&error];
        NSArray *nodes = NULL;
        nodes = [rssParser nodesForXPath:@"//rss/channel/item/title" error:nil];
        for (CXMLElement *title in nodes) {
            [titles addObject:[title stringValue]];
        }

        nodes = NULL;
        nodes = [rssParser nodesForXPath:@"//rss/channel/item/enclosure" error:nil];
        for (CXMLElement *url in nodes) {
            [urls addObject:[[url attributeForName:@"url"] stringValue]];
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            activityIndicator.stopAnimating;
        });
    }
}

现在,UIActivityIndi​​cator出现了,但是单元格是空的。当我不使用dispatch_queue_t时,它运行良好..

有人有想法吗?

提前谢谢!

1 个答案:

答案 0 :(得分:2)

您需要重新加载表格视图(在隐藏活动指示符的同一块中):

[self.tableView reloadData]