重新加载CollectionView不会清除以前加载的单元格 [英] Reloading CollectionView does not clear previously loaded cells

查看:148
本文介绍了重新加载CollectionView不会清除以前加载的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用,该应用利用RestKit 0.20.1从Restful Web服务检索数据。我还应该添加使用CoreData的应用程序。启动应用程序时,主屏幕是一个集合视图,该视图由默认搜索词填充。标头中还有一个文本字段,用作搜索栏。

I have an iOS app that utilizes RestKit 0.20.1 to retrieve data from a Restful web service. I should also add the app uses CoreData. When the app is started the main screen is a collection view that is populated by a default search term. There is also a textfield in the header that is used as a search bar.

当用户使用搜索栏时,我很难清除以前加载的单元格。它只是加载新的单元格并向下推以前的单元格。这是适用的代码。

I am having trouble clearing out the previously loaded cells when the user uses the search bar. It just loads the new cells and pushes the previous ones down. Here is the applicable code.

- (BOOL) textFieldShouldReturn:(UITextField *)textField {

//This sets up the NSDictionary for the Restkit postObject parameters
    NSArray *objects =[NSArray arrayWithObjects:textField.text, nil];
    NSArray *keys =[NSArray arrayWithObjects: @"query",nil];
    NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    self.search=params;

//This is the POST request to the server
    [[RKObjectManager sharedManager] postObject:nil path:@"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];

//This is what I thought would clear out the old and replace with the new    
    [self.collectionView reloadData];    

    [textField resignFirstResponder];
    return YES; 
}

我引用了这个问题如何删除所有项目并将新项目添加到UICollectionView? [collectionView reloadData] 是接受的答案。

I referenced this question How to remove all items and add new items to UICollectionView? and [collectionView reloadData] was the accepted answer.

我选择了 textFieldShouldReturn:本教程中的c $ c>方法。
我还引用了在苹果开发人员库中插入,删除和移动节项目,但我不太确定如何实现删除项目方法。

I chose the textFieldShouldReturn: method from this tutorial. I have also referenced Inserting, Deleting, and Moving Section Items in the apple developer library but I'm not quite sure how to implement the delete items methods.

任何帮助都会很棒。显然,这是一个菜鸟问题,因此代码段非常有帮助。

Any help would be great. This is clearly a rookie question so code snippets are VERY helpful.

更新:
这就是我如何使其工作的方式。

UPDATE: Here is how I got it to work.

postObject deleteAllEntitiesForName 方法中添加了一个调用上面的代码中的$ c>方法和 [self.collectionView reloadData] 语句。

Added a call to the deleteAllEntitiesForName method shown below before the postObject method and [self.collectionView reloadData] statements in the code above.

- (void) deleteAllEntitiesForName:(NSString*)entityName {
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName:entityName inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    NSError *error = nil;
    NSArray *array = [moc executeFetchRequest:request error:&error];
    if (array != nil) {
        for(NSManagedObject *managedObject in array) {
            [moc deleteObject:managedObject];
        }
        error = nil;
        [moc save:&error];
    }

}

我从这里得到的:< a href = https://stackoverflow.com/questions/4910063/correct-way-to-refresh-core-data-database>正确的方法来刷新核心数据数据库

推荐答案

UICollectionView 的经验(不是RestKit的经验),您可能需要先清除数据源调用 reloadData 。之所以会产生这种效果,是因为用于确定集合视图中项目和节的数量的源中仍然包含旧数据。

From experience with UICollectionView (not with RestKit), you probably need to clear out your data source before calling reloadData. The reason you're having this effect is that the source that you use to determine the number of items and sections in the collection view still has old data in it.

A一种简单的验证方法是在调用 reloadData 之前先对数据源的内容进行NSLog。

A simple way to verify this is to NSLog the contents of the data source just before calling reloadData.

希望这会有所帮助!

这篇关于重新加载CollectionView不会清除以前加载的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆