0

即時消息試圖做的是解析XML數據並將其顯示在我的UITableView中,但事情是它顯示像2-3秒後,即時嘗試包括一個UIActivityIndi​​cator當數據加載時,還即時嘗試包括一個GCD,但事情是即時通訊新的東西,所以我真的混淆了做什麼或我在哪裏posible把GCD代碼。顯示提取XML的UIActivityIndi​​cator

這是我的.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 

回答

0

根據XMLParser的工作原理,您當前的CGD塊可以正常工作。您通常不會使用performSelectorOnMainThread將UI交互返回到主線程,但這只是一個常見的約定事項。

大部分問題可能來自dataWithContentsOfURL:,您在cellForRowAtIndexPath:的主線程中調用該問題。你可能想看看SDWebImage來幫助你,但你也可以用GCD自己做。

+0

不知道最新的問題,但它仍然只是輸出一個空白的觀點。並且即時通訊也認爲插入viewDidLoad的gcd是導致問題的原因之一,因爲我試圖輸出有多少行是由NSLog在numOfRowsInSection中返回的,但它給出了0,意味着沒有數據被獲取。 –

0

cellForRowAtIndexPath不會因爲XML完成加載而被調用。在你的代碼塊中,我建議在調用stopAnimating之前,你在你的UITableView上調用reloadData;這將觸發一系列的呼叫,首先到numberOfRowsInSection,然後再到cellForRowAtIndexPath。嘗試在這些函數中放置斷點,以便您可以看到執行流程。

相關問題