当我拉刷新时,Xcode 表视图数据变得重复

Por*_*nel 1 xcode ios pull-to-refresh xcode6

嗨,每次我使用拉动刷新时,我的数据都会重复。例如。通过一次拉动刷新,表视图中的 5 个初始条目将增加一倍至 10 个,并在随后的拉动刷新中再添加 5 个条目。我怎样才能停止重复..我想确保只下载新项目,并且不会再次下载表中的现有数据。

\n\n
@implementation RootViewController\n@synthesize allEntries = _allEntries;\n@synthesize feeds = _feeds;\n@synthesize queue = _queue;\n@synthesize webViewController = _webViewController;\n\n#pragma mark -\n#pragma mark View lifecycle\n\n- (void)addRows {\n\n    RSSEntry *entry1 = [[[RSSEntry alloc] initWithBlogTitle:@"1"\n                                               articleTitle:@"1"\n                                                 articleUrl:@"1"\n                                                articleDate:[NSDate date]] autorelease];\n    RSSEntry *entry2 = [[[RSSEntry alloc] initWithBlogTitle:@"2"\n                                               articleTitle:@"2"\n                                                 articleUrl:@"2"\n                                                articleDate:[NSDate date]] autorelease];\n    RSSEntry *entry3 = [[[RSSEntry alloc] initWithBlogTitle:@"3"\n                                               articleTitle:@"3"\n                                                 articleUrl:@"3"\n                                                articleDate:[NSDate date]] autorelease];\n\n\n    [_allEntries insertObject:entry1 atIndex:0];\n    [_allEntries insertObject:entry2 atIndex:0];\n    [_allEntries insertObject:entry3 atIndex:0];\n\n}\n\n\n\n\n- (void)viewDidLoad {\n    [super viewDidLoad];    \n    self.title = @"Songs";\n    self.allEntries = [NSMutableArray array];\n    self.queue = [[[NSOperationQueue alloc] init] autorelease];\n    self.feeds = [NSArray arrayWithObjects:\n                  @"http://hindisongs.bestofindia.co/?feed=rss2",\n                                 nil];    \n\n\n    self.refreshControl = [UIRefreshControl new];\n    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];\n    [self.refreshControl addTarget:self action:@selector(bindDatas) forControlEvents:UIControlEventValueChanged];\n\n    [self bindDatas]; //called at the first time\n}\n\n-(void)bindDatas\n{\n\n    //GET YOUR DATAS HERE\xe2\x80\xa6\n    for (NSString *feed in _feeds) {\n        NSURL *url = [NSURL URLWithString:feed];\n        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];\n        [request setDelegate:self];\n        [_queue addOperation:request];\n    }\n\n    //update the tableView\n    [self.tableView reloadData];\n\n    if(self.refreshControl != nil && self.refreshControl.isRefreshing == TRUE)\n    {\n        [self.refreshControl endRefreshing];\n\n    }\n}\n\n\n\n\n- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {\n\n    NSArray *channels = [rootElement elementsForName:@"channel"];\n    for (GDataXMLElement *channel in channels) {            \n\n        NSString *blogTitle = [channel valueForChild:@"title"];                    \n\n        NSArray *items = [channel elementsForName:@"item"];\n        for (GDataXMLElement *item in items) {\n\n            NSString *articleTitle = [item valueForChild:@"title"];\n            NSString *articleUrl = [item valueForChild:@"link"];            \n            NSString *articleDateString = [item valueForChild:@"pubDate"];        \n            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];\n\n            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle \n                                                      articleTitle:articleTitle \n                                                        articleUrl:articleUrl \n                                                       articleDate:articleDate] autorelease];\n            [entries addObject:entry];\n\n        }      \n    }\n\n}\n\n- (void)parseAtom:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {\n\n    NSString *blogTitle = [rootElement valueForChild:@"title"];                    \n\n    NSArray *items = [rootElement elementsForName:@"entry"];\n    for (GDataXMLElement *item in items) {\n\n        NSString *articleTitle = [item valueForChild:@"title"];\n        NSString *articleUrl = nil;\n        NSArray *links = [item elementsForName:@"link"];        \n        for(GDataXMLElement *link in links) {\n            NSString *rel = [[link attributeForName:@"rel"] stringValue];\n            NSString *type = [[link attributeForName:@"type"] stringValue]; \n            if ([rel compare:@"alternate"] == NSOrderedSame && \n                [type compare:@"text/html"] == NSOrderedSame) {\n                articleUrl = [[link attributeForName:@"href"] stringValue];\n            }\n        }\n\n        NSString *articleDateString = [item valueForChild:@"updated"];        \n        NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC3339];\n\n        RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle \n                                                  articleTitle:articleTitle \n                                                    articleUrl:articleUrl \n                                                   articleDate:articleDate] autorelease];\n        [entries addObject:entry];\n\n    }      \n\n}\n\n- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {    \n    if ([rootElement.name compare:@"rss"] == NSOrderedSame) {\n        [self parseRss:rootElement entries:entries];\n    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {                       \n        [self parseAtom:rootElement entries:entries];\n    } else {\n        NSLog(@"Unsupported root element: %@", rootElement.name);\n    }    \n}\n\n- (void)requestFinished:(ASIHTTPRequest *)request {\n\n    [_queue addOperationWithBlock:^{\n\n        NSError *error;\n        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] \n                                                               options:0 error:&error];\n        if (doc == nil) { \n            NSLog(@"Failed to parse %@", request.url);\n        } else {\n\n            NSMutableArray *entries = [NSMutableArray array];\n            [self parseFeed:doc.rootElement entries:entries];                \n\n            [[NSOperationQueue mainQueue] addOperationWithBlock:^{\n\n                for (RSSEntry *entry in entries) {\n\n                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {\n                        RSSEntry *entry1 = (RSSEntry *) a;\n                        RSSEntry *entry2 = (RSSEntry *) b;\n                        return [entry1.articleDate compare:entry2.articleDate];\n                    }];\n\n                    [_allEntries insertObject:entry atIndex:insertIdx];\n                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]\n                                          withRowAnimation:UITableViewRowAnimationRight];\n\n                }                            \n\n            }];\n\n        }        \n    }];\n\n}\n\n- (void)requestFailed:(ASIHTTPRequest *)request {\n    NSError *error = [request error];\n    NSLog(@"Error: %@", error);\n}\n\n\n/*\n- (void)viewWillAppear:(BOOL)animated {\n    [super viewWillAppear:animated];\n}\n*/\n/*\n- (void)viewDidAppear:(BOOL)animated {\n    [super viewDidAppear:animated];\n}\n*/\n/*\n- (void)viewWillDisappear:(BOOL)animated {\n    [super viewWillDisappear:animated];\n}\n*/\n/*\n- (void)viewDidDisappear:(BOOL)animated {\n    [super viewDidDisappear:animated];\n}\n*/\n\n/*\n // Override to allow orientations other than the default portrait orientation.\n- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {\n    // Return YES for supported orientations.\n    return (interfaceOrientation == UIInterfaceOrientationPortrait);\n}\n */\n\n\n#pragma mark -\n#pragma mark Table view data source\n\n// Customize the number of sections in the table view.\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {\n    return 1;\n}\n\n\n// Customize the number of rows in the table view.\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {\n    return [_allEntries count];\n}\n\n\n// Customize the appearance of table view cells.\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {\n\n    static NSString *CellIdentifier = @"Cell";\n\n    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];\n    if (cell == nil) {\n        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];\n    }\n\n    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];\n\n    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];\n    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];\n    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];\n    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];\n\n    cell.textLabel.text = entry.articleTitle;        \n    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];\n\n    return cell;\n}\n\n\n/*\n// Override to support conditional editing of the table view.\n- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {\n    // Return NO if you do not want the specified item to be editable.\n    return YES;\n}\n*/\n\n\n/*\n// Override to support editing the table view.\n- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {\n\n    if (editingStyle == UITableViewCellEditingStyleDelete) {\n        // Delete the row from the data source.\n        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];\n    }   \n    else if (editingStyle == UITableViewCellEditingStyleInsert) {\n        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.\n    }   \n}\n*/\n\n\n/*\n// Override to support rearranging the table view.\n- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {\n}\n*/\n\n\n/*\n// Override to support conditional rearranging of the table view.\n- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {\n    // Return NO if you do not want the item to be re-orderable.\n    return YES;\n}\n*/\n\n\n#pragma mark -\n#pragma mark Table view delegate\n\n- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {\n\n    if (_webViewController == nil) {\n        self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];\n    }\n    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];\n    _webViewController.entry = entry;\n    [self.navigationController pushViewController:_webViewController animated:YES];\n\n}\n\n#pragma mark -\n#pragma mark Memory management\n\n- (void)didReceiveMemoryWarning {\n    // Releases the view if it doesn\'t have a superview.\n    [super didReceiveMemoryWarning];\n\n    // Relinquish ownership any cached data, images, etc that aren\'t in use.\n    self.webViewController = nil;\n}\n\n- (void)viewDidUnload {\n    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.\n    // For example: self.myOutlet = nil;\n}\n\n\n- (void)dealloc {\n    [_allEntries release];\n    _allEntries = nil;\n    [_queue release];\n    _queue = nil;\n    [_feeds release];\n    _feeds = nil;\n    [_webViewController release];\n    _webViewController = nil;\n    [super dealloc];\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Kri*_*lci 5

问题是您不断将对象插入到 _allEntries 中而不重置它。您可能希望在某个时刻清空它,无论是当用户拉动刷新时,还是在向其中添加任何新对象之前有新数据进入时。

[_allEntries removeAllObjects];
Run Code Online (Sandbox Code Playgroud)

尝试将其放在bindDatas 的开头。