2014-10-20 87 views
0

我有一个PFQueryTableViewController设置为在加载视图时检索满足一组基本约束的对象列表下面是实现查询的方法。在PFQueryTableViewController中筛选查询结果

- (PFQuery *)queryForTable { 
    //The base query. 
    self.query = [PFQuery queryWithClassName:self.parseClassName]; 
    [self.query includeKey:@"photos"]; 
    [self.query includeKey:@"user"]; 
    [self.query orderByDescending:@"createdAt"]; 
    [self.query whereKey:@"ordered" equalTo:[NSNumber numberWithBool:false]]; 
//  [query whereKey:@"location" nearGeoPoint:self.currentLocation withinKilometers:32.2]; 
    // If Pull To Refresh is enabled, query against the network by default. 
    if (self.pullToRefreshEnabled) { 
     self.query.cachePolicy = kPFCachePolicyNetworkOnly; 
    } 

    // If no objects are loaded in memory, we look to the cache first to fill the table 
    // and then subsequently do a query against the network. 
    if (self.objects.count == 0) { 
     self.query.cachePolicy = kPFCachePolicyCacheThenNetwork; 
    } 


    return self.query; 
} 

但是,我也想给用户过滤结果的选项。 我用来尝试和过滤的代码不起作用,因为对象列表不会改变。

[self clear]; 
[self.query whereKey:@"cuisine" equalTo:cuisine.text]; 
[self loadObjects]; 

这些指令嵌入UIAlertController包含UITextField称为cuisine

为什么没有在过滤器经历?

感谢, 亚洲时报Siddharth

回答

1

每次执行装入对象它将通过queryForTable得到一个新的查询:

试试这个:

-(void)search 
{ 
    [self clear]; 

    //include this if you want to clear the query before applying the new filter 
    self.query = [self startingQuery]; 
    [self.query whereKey:@"cuisine" equalTo:cuisine.text]; 
    [self loadObjects]; 
} 
-(PFQuery *)query 
{ 
    if (!_query) _query = [self startingQuery]; 
    return _query; 
} 

- (PFQuery *)queryForTable { 
    return self.query; 
} 
-(PFQuery *)startingQuery 
{ 
    //The base query. 
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName]; 
    [query includeKey:@"photos"]; 
    [query includeKey:@"user"]; 
    [query orderByDescending:@"createdAt"]; 
    [query whereKey:@"ordered" equalTo:[NSNumber numberWithBool:false]]; 
    //  [query whereKey:@"location" nearGeoPoint:self.currentLocation withinKilometers:32.2]; 
    // If Pull To Refresh is enabled, query against the network by default. 
    if (self.pullToRefreshEnabled) { 
     query.cachePolicy = kPFCachePolicyNetworkOnly; 
    } 

    // If no objects are loaded in memory, we look to the cache first to fill the table 
    // and then subsequently do a query against the network. 
    if (self.objects.count == 0) { 
     query.cachePolicy = kPFCachePolicyCacheThenNetwork; 
    } 


    return query; 
}