在获取XML时显示UIActivityIndi​​cator

时间:2013-08-21 14:00:27

标签: ios uitableview ios6 grand-central-dispatch uiactivityindicatorview

我想要做的是解析XML数据并在我的UITableView中显示它,但事情是它显示在2-3秒之后,我试图在数据加载时包含一个UIActivityIndi​​cator并且我试图包含一个gcd ,但事情是这个东西的新东西,所以我真的很混淆要做什么或我在哪里放置gcd代码。

这是我的.m文件。

@implementation ViewController
@synthesize listTableView;

dispatch_queue_t myQueue;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            return [[xmlParser listPopulated]count];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"CustomCell";

    dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
       CustomCellXMLClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {
        cell = [[CustomCellXMLClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellXMLSample" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }



        NSString *nameLabel = [currentData nameOfCat];
        NSString *dataToCacheLabel = [myCache objectForKey:nameLabel];
        if(nameLabel != nil){
            dataToCacheLabel = [NSString stringWithString:nameLabel];
                if (dataToCacheLabel != nil) {
                    [myCache setObject:dataToCacheLabel forKey:nameLabel];
                    [cell.nameLabel setText:dataToCacheLabel];

                }
        }


        NSString *detailLabel = [currentData descriptionOfCat];
        NSString *stringToCache = [myCache objectForKey:detailLabel];
        if (detailLabel != nil) {
            stringToCache = [NSString stringWithString:detailLabel];
                if (stringToCache != nil) {
                    [myCache setObject:stringToCache forKey:detailLabel];
                    [cell.detailLabel setText:stringToCache];
                }
        }

        NSString *imageURL = [currentData imageLink];
        NSData *dataToCache;
            if (imageURL != nil) {

                dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
                if (dataToCache != nil) {
                    [myCache setObject:dataToCache forKey:imageURL];
                    [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
                }
                else {
                    NSURL *imageURL = [NSURL URLWithString:@"http://i178.photobucket.com/albums/w255/ace003_album/190579604m.jpg"];
                    dataToCache = [NSData dataWithContentsOfURL:imageURL];
                    [myCache setObject:dataToCache forKey:imageURL];
                    [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
                }

            }



    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *title = @"Sample View";
    return title;

}
- (void)viewDidLoad
{
    [super viewDidLoad];

[self.activityIndicator startAnimating];
self.activityIndicator.hidesWhenStopped = YES;



dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myQueue, ^ {
    xmlParser = [[XMLParser alloc]loadXMLByURL:@"http://www.irabwah.com/mobile/core.php?cat=0"];
    [self.activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
});

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:0)

根据XMLParser的工作原理,您当前的CGD块可以正常使用。您通常不会使用performSelectorOnMainThread将UI交互返回到主线程,但这只是一个常见的约定事项。

您的大多数问题可能来自dataWithContentsOfURL:,您正在cellForRowAtIndexPath:的主要主题上调用。您可能需要查看SDWebImage来帮助解决这个问题,但您也可以使用GCD自行完成。

答案 1 :(得分:0)

不会因为您的XML加载完成而调用<{cellForRowAtIndexPath。在您的代码块中,我建议您在调用stopAnimating之前在UITableView上调用reloadData;这将触发一系列调用,首先是numberOfRowsInSection,然后是cellForRowAtIndexPath。尝试在这些函数中加入断点,以便查看执行流程。