如何刷新UICollectionView以反映最近对我的dataSource所做的更改?

时间:2014-04-29 11:55:32

标签: ios objective-c cocoa-touch uitableview uicollectionview

我正在应用中使用过滤器的功能,使用它们的价格对项目进行排序。我希望让客户能够使用UIPickerView按价格从高到低或从低到高对商品进行分类。

了解它的外观:

enter image description here

问题:

我遇到的问题是在查询了parse.com数据库并获得新排序的数据后,重新加载UICollectionView的数据无效。我发现这令人困惑,因为我正在更改我的实际dataSource(存储数据库结果的NSArray)。所以我收集了重新加载数据,UICollectionView将获取最新结果。但是没有任何事情发生。

以下是我的设置方式:

跟踪UIPickerView(过滤器)是否已激活:

@implementation VAGGarmentsCollectionViewController
{
    BOOL _filterActivated;
}

点击过滤器以打开UIPickerView set _filterActivated instance var为yes:

- (void)filterButtonTapped
{    
    // I have all code in here to create the UIPickerView
    // Deleted it all to make the post clearer    

    _filterActivated = YES;
}

设置UIPickerView行的标题:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_filterPickerArray objectAtIndex:row];
}

- (void)loadTitleForIndex:(NSInteger)index
{
    if (index == 0) {
        [_filterButton setTitle:@"Recommend" forState:UIControlStateNormal];
    } else if (index == 1) {
        [_filterButton setTitle:@"What's New" forState:UIControlStateNormal];
    } else if (index == 2) {
        [_filterButton setTitle:@"High to Low" forState:UIControlStateNormal];
    } else if (index == 3) {
        [_filterButton setTitle:@"Low to High" forState:UIControlStateNormal];
    }
}

检测UIPickerView中特定行的选择:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSIndexPath *selectedObjectIndex = [_filterPickerArray objectAtIndex:row];
    NSInteger indexOfSelectedObject = [_filterPickerArray indexOfObject:selectedObjectIndex];

    //Let's print in the console what the user had chosen;
    NSLog(@"Chosen item: %@", [_filterPickerArray objectAtIndex:row]);

    [_thisController loadTitleForIndex:indexOfSelectedObject];

    _selectedRowInFilterPicker = [_filterPicker selectedRowInComponent:0];
}

点击完成按钮时检测:

- (void)pickerDoneButtonTapped
{
    NSLog(@"Picker done button pressed");

    [_thisController performQuery];
    [_collectionView reloadData];

    _filterActivated = NO;
}

然后我有一个performQuery方法,在从parse.com获取后台数据之前调用queryForCollection方法来接受任何其他查询选项

查询收集方法:

-(PFQuery *)queryForCollection
{
    PFQuery *query = [PFQuery queryWithClassName:@"Garments"];
    if (_filterActivated) {
        // check what the selectedRowInFilterPicker currently is
        // then add that task to the query
        NSLog(@"FILTER WAS ACTIVATED");

        if (_selectedRowInFilterPicker == 0) {

        } else if (_selectedRowInFilterPicker == 1) {

        } else if (_selectedRowInFilterPicker == 2) {
            NSLog(@"ORDER BY DESCENDING USING PRICE");
            [query orderByDescending:@"price"];
        } else if (_selectedRowInFilterPicker == 3) {
            NSLog(@"ORDER BY ASCENDING USING PRICE");
            [query orderByAscending:@"price"];
        }
    }
    return query;
}

执行查询方法:

- (void)performQuery
{
    NSLog(@"PERFORM QUERY");
    PFQuery *query = self.queryForCollection;

    if (_paginationEnabled) {
        [query setLimit:_objectsPerPage];
        //fetching the next page of objects
        if (!_isRefreshing) {
            [query setSkip:self.objects.count];
        }
    }


    [self objectsWillLoad];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {


        self.isLoading = NO;
        if (error)
            self.objects = [NSArray new];
        else {
            if (_paginationEnabled && !_isRefreshing) {
                //add a new page of objects
                NSMutableArray *mutableObjects = [NSMutableArray arrayWithArray:self.objects];
                [mutableObjects addObjectsFromArray:objects];
                self.objects = [NSArray arrayWithArray:mutableObjects];
            }
            else {
                self.objects = objects;
            }
        }


        [self objectsDidLoad:error];
    }];
}

打破我正在做的事情:

  • 使用“filterButtonTapped”方法激活检测UIPickerView,将“_filterActivated”实例var设置为yes。

  • 点击完成按钮后执行查询。 performQuery 方法调用 queryForCollection 方法。在该方法中,我检查“_filterActivated”是否设置为yes,如果是,则检查UIPickerView中的哪一行被选中,并为查询添加适当的查询选项。

  • 我希望在 performQuery 方法完成后重新加载UICollectionView的数据。什么都没发生。我已经尝试在索引路径上重新加载可见的项目和项目,但我仍然一无所获。

也许我会以错误的方式解决这个问题。我睡着了,以为我今天下午会再尝试一下,但还是看不出我哪里出错了。

我想做什么:

我想要做的就是在UICollectionView中选择过滤器选项/行后重新加载整个UIPickerView(例如,从低到高排序的项目)并点击完成{{ 1 {} UIButton

更新

UIPickerView

1 个答案:

答案 0 :(得分:0)

您在请求新数据之后但在实际收到新数据之前调用reloadData。

在performQuery返回后调用传递给findObjectsInBackgroundWithBlock的块,并在pickerDoneButtonTapped中调用了reloadData。

reloadData放在您传递的findObjectsInBackgroundWithBlock块的末尾,或objectsDidLoad:方法中。