2015-08-07 7 views
0

Salut Mes données sont dupliquées chaque fois que j'utilise le pull pour rafraîchir .. par exemple. 5 entrées initiales dans la vue de table sont doublées à 10 avec un tirage pour actualiser et ajouter 5 autres dans le tirage suivant pour actualiser. Comment puis-je arrêter la duplication? Je voudrais m'assurer que seuls les nouveaux éléments sont téléchargés et que les données existantes dans la table ne sont pas téléchargées à nouveau.Xcode Vue de la table Les données sont dupliquées lorsque je tire pour rafraîchir

@implementation RootViewController 
@synthesize allEntries = _allEntries; 
@synthesize feeds = _feeds; 
@synthesize queue = _queue; 
@synthesize webViewController = _webViewController; 

#pragma mark - 
#pragma mark View lifecycle 

- (void)addRows { 

    RSSEntry *entry1 = [[[RSSEntry alloc] initWithBlogTitle:@"1" 
               articleTitle:@"1" 
               articleUrl:@"1" 
               articleDate:[NSDate date]] autorelease]; 
    RSSEntry *entry2 = [[[RSSEntry alloc] initWithBlogTitle:@"2" 
               articleTitle:@"2" 
               articleUrl:@"2" 
               articleDate:[NSDate date]] autorelease]; 
    RSSEntry *entry3 = [[[RSSEntry alloc] initWithBlogTitle:@"3" 
               articleTitle:@"3" 
               articleUrl:@"3" 
               articleDate:[NSDate date]] autorelease]; 


    [_allEntries insertObject:entry1 atIndex:0]; 
    [_allEntries insertObject:entry2 atIndex:0]; 
    [_allEntries insertObject:entry3 atIndex:0]; 

} 




- (void)viewDidLoad { 
    [super viewDidLoad];  
    self.title = @"Songs"; 
    self.allEntries = [NSMutableArray array]; 
    self.queue = [[[NSOperationQueue alloc] init] autorelease]; 
    self.feeds = [NSArray arrayWithObjects: 
        @"http://hindisongs.bestofindia.co/?feed=rss2", 
           nil];  


    self.refreshControl = [UIRefreshControl new]; 
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"]; 
    [self.refreshControl addTarget:self action:@selector(bindDatas) forControlEvents:UIControlEventValueChanged]; 

    [self bindDatas]; //called at the first time 
} 

-(void)bindDatas 
{ 

    //GET YOUR DATAS HERE… 
    for (NSString *feed in _feeds) { 
     NSURL *url = [NSURL URLWithString:feed]; 
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
     [request setDelegate:self]; 
     [_queue addOperation:request]; 
    } 

    //update the tableView 
    [self.tableView reloadData]; 

    if(self.refreshControl != nil && self.refreshControl.isRefreshing == TRUE) 
    { 
     [self.refreshControl endRefreshing]; 

    } 
} 




- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries { 

    NSArray *channels = [rootElement elementsForName:@"channel"]; 
    for (GDataXMLElement *channel in channels) {    

     NSString *blogTitle = [channel valueForChild:@"title"];      

     NSArray *items = [channel elementsForName:@"item"]; 
     for (GDataXMLElement *item in items) { 

      NSString *articleTitle = [item valueForChild:@"title"]; 
      NSString *articleUrl = [item valueForChild:@"link"];    
      NSString *articleDateString = [item valueForChild:@"pubDate"];   
      NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822]; 

      RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                 articleTitle:articleTitle 
                 articleUrl:articleUrl 
                 articleDate:articleDate] autorelease]; 
      [entries addObject:entry]; 

     }  
    } 

} 

- (void)parseAtom:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries { 

    NSString *blogTitle = [rootElement valueForChild:@"title"];      

    NSArray *items = [rootElement elementsForName:@"entry"]; 
    for (GDataXMLElement *item in items) { 

     NSString *articleTitle = [item valueForChild:@"title"]; 
     NSString *articleUrl = nil; 
     NSArray *links = [item elementsForName:@"link"];   
     for(GDataXMLElement *link in links) { 
      NSString *rel = [[link attributeForName:@"rel"] stringValue]; 
      NSString *type = [[link attributeForName:@"type"] stringValue]; 
      if ([rel compare:@"alternate"] == NSOrderedSame && 
       [type compare:@"text/html"] == NSOrderedSame) { 
       articleUrl = [[link attributeForName:@"href"] stringValue]; 
      } 
     } 

     NSString *articleDateString = [item valueForChild:@"updated"];   
     NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC3339]; 

     RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                articleTitle:articleTitle 
                articleUrl:articleUrl 
                articleDate:articleDate] autorelease]; 
     [entries addObject:entry]; 

    }  

} 

- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {  
    if ([rootElement.name compare:@"rss"] == NSOrderedSame) { 
     [self parseRss:rootElement entries:entries]; 
    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {      
     [self parseAtom:rootElement entries:entries]; 
    } else { 
     NSLog(@"Unsupported root element: %@", rootElement.name); 
    }  
} 

- (void)requestFinished:(ASIHTTPRequest *)request { 

    [_queue addOperationWithBlock:^{ 

     NSError *error; 
     GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                   options:0 error:&error]; 
     if (doc == nil) { 
      NSLog(@"Failed to parse %@", request.url); 
     } else { 

      NSMutableArray *entries = [NSMutableArray array]; 
      [self parseFeed:doc.rootElement entries:entries];     

      [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 

       for (RSSEntry *entry in entries) { 

        int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) { 
         RSSEntry *entry1 = (RSSEntry *) a; 
         RSSEntry *entry2 = (RSSEntry *) b; 
         return [entry1.articleDate compare:entry2.articleDate]; 
        }]; 

        [_allEntries insertObject:entry atIndex:insertIdx]; 
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]] 
              withRowAnimation:UITableViewRowAnimationRight]; 

       }        

      }]; 

     }   
    }]; 

} 

- (void)requestFailed:(ASIHTTPRequest *)request { 
    NSError *error = [request error]; 
    NSLog(@"Error: %@", error); 
} 


/* 
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 
*/ 
/* 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 
*/ 
/* 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 
*/ 
/* 
- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 


#pragma mark - 
#pragma mark Table view data source 

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [_allEntries count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 

    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate]; 

    cell.textLabel.text = entry.articleTitle;   
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle]; 

    return cell; 
} 


/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 


/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
*/ 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 


#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (_webViewController == nil) { 
     self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease]; 
    } 
    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 
    _webViewController.entry = entry; 
    [self.navigationController pushViewController:_webViewController animated:YES]; 

} 

#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
    self.webViewController = nil; 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [_allEntries release]; 
    _allEntries = nil; 
    [_queue release]; 
    _queue = nil; 
    [_feeds release]; 
    _feeds = nil; 
    [_webViewController release]; 
    _webViewController = nil; 
    [super dealloc]; 
} 
+0

Veuillez publier le code pour numberOfRowsInSection, cellForRowAtIndexPath et - (void) requestFinished: (ASIHTTPRequest *) request. Merci! –

+0

vient d'ajouter le code restant –

Répondre

4

Le problème est que vous continuez à insérer l'objet dans _allEntries sans jamais le réinitialiser. Vous voudrez le vider à un moment donné, soit lorsque l'utilisateur tire pour actualiser ou lorsque les nouvelles données entrent avant d'ajouter de nouveaux objets.

[_allEntries removeAllObjects]; 

Essayez de le mettre au début de bindDatas.

+0

Merci beaucoup Kris .. Appréciez votre aide .. Qui a résolu le problème –