0

我试图做的是解析 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
4

2 回答 2

0

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

您的大部分问题可能来自dataWithContentsOfURL:您在cellForRowAtIndexPath:. 您可能想看看SDWebImage以帮助解决这个问题,但您也可以使用 GCD 自己完成。

于 2013-08-21T14:16:04.807 回答
0

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

于 2013-08-22T07:09:31.850 回答